Reputation: 861
I have this function :
void change(int **x, int **y){
x = y;
}
int main(void){
int x = 10 , y = 15;
int *p = &x;
int *q = &y;
change(&p, &q);
printf("%d %d", x, y);
return 0;
}
It still outputs the same value.
Why is it that y
was not assign on x
?
Upvotes: 0
Views: 138
Reputation: 304
Let's take an example:
int x = 10 , y = 15;
int *p = &x;
int *q = &y;
Suppose &x
represents memory address 100 and &y
represents memory address 104.
Then according to your program p
will have 100 and q
will have 104 address pointed.
Forgive me for one more assumption :)
p
and q
will also be having their some memory address.
Hence,suppose &p
represents memory address 200 and &q
represents memory address 204.
change(&p, &q);
void change(int **x, int **y){
x = y;
}
Now here x
will point to &p
and y
will point to &q
.
now using x=y
will make the x to point to y i.e. x
and y
both pointing to 204 memory address ,which by any means cannot change the value of x
(x=10) and y
(y=15) in the following function snippet.
int main(void){
int x = 10 , y = 15;
int *p = &x;
int *q = &y;
change(&p, &q);
printf("%d %d", x, y);
return 0;
}
Upvotes: 1
Reputation: 343
Say You have variable x stored in location 2015 and y stored in 2016. Now this pointers are stored in locations 2020 and 2021 respectively. So when you call the function change values are like this:
x = 10
y = 15
p = 2015
q = 2016
int **x OR &p = 2020
int **y OR &q = 2021
Now what you are doing is like &p = &q, which is nothing but changing values of int **x to 2021. but values of location 2015 and 2016 remains the same.
x = 10
y = 15
p = 2015
q = 2016
int **x OR &p = 2021
int **y OR &q = 2021
this has nothing to do with values of original x
or y
.
Upvotes: 1
Reputation: 1227
in your function ,you only change the copy. try this
#include <stdio.h>
#include <stdlib.h>
void change(int **x, int **y){
**x = **y;
}
int main(void){
int x = 10 , y = 15;
int *p = &x;
int *q = &y;
change(&p, &q);
printf("%d %d", x, y);
return 0;
}
Upvotes: 1
Reputation: 1000
int **x, int **y is just a declaration of double pointer. now x and y will contain the address of pointer p and q. **x=**y should work.
Upvotes: 0
Reputation: 8657
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);
Upvotes: 3