简体   繁体   中英

Why is this C function not doing anything?

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 ?

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);

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;

}

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 .

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 ie 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;

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM