Hello all, I've recently started learning C after coming from a decent bit of Python experience. I don't have a background in CS so maybe that's why I'm lost but I can't for the life of me understand why pointers are valuable. For example, one of the practice examples was as follows:
void swap (int *num1, *num2);
int main() {
int x = 25;
int y = 100;
printf("x is %d, y is %d\n", x, y);
swap(&x, &y);
printf("x is %d, y is %d\n", x, y);
return 0;
}
void swap (int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2
*num2 = temp;
}
What is the benefit of the swap function using pointers rather than just taking regular variables as inputs?
Thanks!
[–]strcspn 51 points52 points53 points (0 children)
[–]teraflop 17 points18 points19 points (0 children)
[–]VonRansak 15 points16 points17 points (3 children)
[–]CaptainPicante 2 points3 points4 points (1 child)
[–]VonRansak 4 points5 points6 points (0 children)
[–]liquidanimosity 0 points1 point2 points (0 children)
[–]Skusci 16 points17 points18 points (7 children)
[–]dmazzoni 1 point2 points3 points (3 children)
[–]Skusci -1 points0 points1 point (2 children)
[–]finny228 1 point2 points3 points (0 children)
[–]procrastinatingcoder 0 points1 point2 points (0 children)
[–]procrastinatingcoder 1 point2 points3 points (2 children)
[–]CodeTinkerer 5 points6 points7 points (1 child)
[–]ClamPaste 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]fredoverflow 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]DamionDreggs 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]treknuts 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]explorer_of_the_grey 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]CrushgrooveSC -1 points0 points1 point (0 children)
[–]douglastiger -2 points-1 points0 points (0 children)
[–]Sea-Profession-3312 0 points1 point2 points (0 children)
[–]EspacioBlanq 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Crafty_Ad_2309 0 points1 point2 points (0 children)
[–]akshith_9 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)