Hi, everyone. I have here a code of a program that interchanges the values of two int variables, x and y, by using pointers:
Code:
#include <stdio.h>
void interchange(int * u, int * v);
int main(void)
{
int x = 5, y = 10;
printf("Originally, x = %d and y = %d.\n", x, y);
interchange(&x, &y); //send addresses to function
printf("Now, x = %d and y = %d.\n", x, y);
return 0;
}
void interchange(int * u , int * v)
{
int temp;
temp = *u; //temp = x //temp gets value that u points to
*u = *v; //x = y
*v = temp; //y = temp
}
The thing is: how are the values of x and y changed in the main function, if the funtion interchange() doesn't return anything? Is it because this one works with pointers that point to x and y, in such a way that these pointers are the "bridge" between the main() and the interchange() functions?
Thanks in advance!