Hello so I am learning pointers in c programming right now and I made a basic call by reference swap function that doesn't work as seen below.
#include <stdio.h>
void swap (int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main(void) {
int a = 10, b = 20;
int *ptr1 = &a, *ptr2 = &b;
printf("before swap: \t %d \t %d\n", a, b);
void swap(&a, &b);
printf("after swap: \t %d \t %d", a, b);
}
int main();
However when I got rid of the void in front of the swap function call (below) it then started to work. So I want to know why this is and why this isn't the case for the main function call (at the bottom) as I have the return type int in front of that.
#include <stdio.h>
void swap (int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main(void) {
int a = 10, b = 20;
int *ptr1 = &a, *ptr2 = &b;
printf("before swap: \t %d \t %d\n", a, b);
swap(&a, &b);
printf("after swap: \t %d \t %d", a, b);
}
int main();