0

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?

DoxyLover
  • 3,366
  • 1
  • 15
  • 19
XDProgrammer
  • 853
  • 2
  • 14
  • 31

5 Answers5

3

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);
a3f
  • 8,517
  • 1
  • 41
  • 46
  • 2
    Indirection on `y` is not needed. Also show how to invoke `change`. – chqrlie Mar 31 '15 at 03:46
  • @chqrlie I assumed he wants to extend it eventually to a `swap` function. But you are right, I should elaborate. – a3f Mar 31 '15 at 03:47
  • Hi thanks for quick reply, I heard a lot about passing by reference and value.. I google them many times by still dont get the idea.. when is passing by reference is actually useful? since C is passing by value anyway why this feature is allowed and does not give you error? – XDProgrammer Mar 31 '15 at 03:49
  • @NoobProgrammer This is one example for its use, without pass-by-reference you couldn't change the value of something outside the function from inside it. Also it saves you from copying data whenever a function needs it. For more information, check this question: http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – a3f Mar 31 '15 at 03:55
  • Im sorry, im just not understanding this phrase clearly "without pass-by-reference you couldn't change the value of something outside the function from inside it."... – XDProgrammer Mar 31 '15 at 04:04
  • @NoobProgrammer we established that when we say f(x), a copy of x is used in the function. if we didn't want a mere copy but the original which was outside the function we need to pass its address (i.e: a reference to it). Check out the link, it has a nice analogy. – a3f Mar 31 '15 at 04:05
1

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;

}
icecity96
  • 1,177
  • 12
  • 26
1

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.

Sushovan
  • 333
  • 1
  • 3
  • 13
1

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;

}

user4291320
  • 302
  • 1
  • 12
0

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.

Karthick
  • 1,010
  • 1
  • 8
  • 24
  • "double pointer" is meaningless. "pointer to pointer" is probably what you intended to say. – mlp Mar 31 '15 at 05:18