Arguments to C functions are always passed by value. In order to enable a pass-by-reference behavior, one passes the pointers by value. These pointers can then be dereferenced to access the values outside the function.
void change(int **x, int **y){
x = y;
}
Here you are assigning the value of y
to x
, which while valid is not what you probably intended: You want the pointed at value to change, not the local copy of it. Also one level of indirection suffices. Thus change it to:
void change(int *x, int *y){
*x = *y;
}
which is used like this:
int a = 2, b = 3;
change(&a, &b);
While this works, it's still suboptimal. We are only changing the value pointed at by x
. The value pointed at by y
is passed as it without change. So we can save that indirection and pass y by value:
void change(int *x, int y){
*x = y;
}
Which is used, as you hopefully can guess now, like this:
int a = 3, b = 4;
change(&a, b);