C pointers - Point to the same address

后端 未结 7 1607
野趣味
野趣味 2021-02-10 15:38
#include 
#include 

void foo(int *a, int *b);

void foo(int *a, int *b) {
    *a = 5;
    *b = 6;
    a = b;
}

int main(void) {
    int          


        
相关标签:
7条回答
  • 2021-02-10 16:02

    Using a pseudo-memory map,

    In main(),

        a      b
     --------------
    |  5   |   6   | <- data
     --------------
     [1000]  [1004]  <- address
    

    In the function foo(),

        a        b      ( local to foo(), different from the a & b in main() )
     ----------------
    |  1000  | 1004  | <- data
     ----------------
      [2000]   [2004]  <- address
    

    So, when in foo()'s scope,

    *a = 5;   // store 5 in int variable a
    *b = 6;   // store 6 in int variable b
     a = b;   // copies contents of pointer variable b to a
    

    So the final map in foo()'s scope is:

        a        b
     ----------------
    |  1004  | 1004  | <- data
     ----------------
      [2000]   [2004]  <- address
    
    0 讨论(0)
  • 2021-02-10 16:07

    Because when foo is called, the values of the pointers are copied into the function. If you want to change the values of the pointers themselves, you need to pass a pointer to a pointer into the function.

    0 讨论(0)
  • 2021-02-10 16:09

    a and b are local to the function foo (they are on the stack), when program returns from the function data on the stack is lost. when you assign b to a, you are only modifying memory addresses on the stack, not their values.

    0 讨论(0)
  • 2021-02-10 16:12

    I'm not sure what you're going after... if it's to get a and b to contain the same value, try *a = *b.

    0 讨论(0)
  • 2021-02-10 16:14

    In foo, a and b are separate local variables. Setting them to have the same value has no effect on the previous values - the last line of foo currently does nothing, basically.

    Within foo, a is initially a pointer to the same location as a in main, and b is a pointer to the same location as b in main. The last line just makes the value of a in foo the same as b - namely a pointer to the same location as b in main. So if you add a line

    *a = 7;
    

    at the end of foo, then you'd see output of "5, 7".

    (Your code would definitely be easier to talk about if you used different variable names in main and foo, by the way.)

    If you're trying to make a and b within main "aliased" to each other, you're not going to be successful. They're separate local variables on the stack, and will remain so. You can't make the stack "shrink" to alias the two, whatever you do.

    0 讨论(0)
  • 2021-02-10 16:15

    It does work; it just doesn't do what you think it does.

    In foo(), a = b changes the pointer a to point to whatever b points to. It has no effect on anything outside of the function; it only changes the pointers.

    If you want to change the value of the int pointed to by a to be the same as the value of the int pointed to by b, you need to use *a = *b, similar to how you do the assignments in the function already.

    0 讨论(0)
提交回复
热议问题