+ 1

i'm learning c pointer , please explain the difference between-->

1.it is c pointer for swaping two numbers #include <stdio.h> void swap (int *num1, int *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; } 2. why this code will not work--> #include <stdio.h> void swap (int num1, int 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; }

11th Nov 2018, 7:38 PM
vansh bhardwaj
vansh bhardwaj - avatar
2 odpowiedzi
+ 4
Directly addresses your problem (2 hours ago -- please search first!) https://www.sololearn.com/Discuss/1582970/what-is-the-problem-in-the-code
11th Nov 2018, 7:52 PM
Babak
Babak - avatar
0
the second will not work bcos the variables declared inside the swap function is not a pointer even if it is the address of x and y that is passed to it.using a pointer allows you to dereference the x and y variable
12th Nov 2018, 9:53 PM
okonkwo calistus