#include <stdio.h>
void swap(int *a, int *b);
int main(void)
{
int x = 1;
int y = 2;
printf("x is %i, y is %i\n", x, y);
swap(&x, &y);
printf("x is %i, y is %i\n", x, y);
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
In this code (I didn't write it btw) I don't understand how x and y are being swapped when we are actually swapping a
and b
in the function below. i do not know what the relationship between 'x and y' and 'a and b' is.
I know that *
is a pointer, and that '&' gives the address to things, but idk how it's pointing to x
and y
when they're ( x
and y
) not mentioned in the actual function.
i don't know what 'placeholder' is being used.
Please explain this if you can!