C pointers - Different address

牧云@^-^@ 提交于 2019-12-11 16:02:52

问题


I'm trying to learn about C pointers but I cannot understand somethings... The following code:

#include <stdio.h>

void foo(int *x, int *y);

void foo(int *x, int *y) {
    printf("x = %p\ny = %p\n", &x, &y);
    *x = 5;
    *y = 6;
}

int main(void) {
    int a, b;
    printf("a = %p\nb = %p\n", &a, &b);
    foo(&a, &b);
    return 0;
}

Why are the addresses different? The first printf (main) output two addresses. the other printf (foo) output different addresses. I'm passing addresses to foo (& operator).


回答1:


In main, you're printing addresses of x and y, but in foo, you're printing addresses of pointers to x and y. You meant to write:

printf("x = %p\ny = %p\n", x, y);

(notice the lack of & before x and y)




回答2:


because printf("x = %p\ny = %p\n", &x, &y); print the address of x and y, instead of their value. You need to replace it with

printf("x = %p\ny = %p\n", x, y);

to yield same results.




回答3:


Your foo function takes pointers to int as parameters, that means that x and y are already pointers in foo and their values are the addresses you want to print.

Instead you print the addresses of these pointers. Your print should look more like

printf("x = %p\ny = %p\n", x, y);



回答4:


The printf call in foo is being passed the addresses of the pointers, i.e., variables of type int **. If you remove the & symbols from the printf call you should get the same addresses as from the printf call in main.




回答5:


First, about your C:

If you are defining the entire function above main(), you need not include its prototype before it. So delete the 2nd line:

void foo(int *x, int *y);

Now, for the real matter at hand, a pseudo memory map would come handy:

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

    x        y
 ----------------
|  1000  | 1004  | <- data
 ----------------
  [2000]   [2004]  <- address

So here, you shall get:

a = 1000 b = 1004  (the addresses of a & b)
x = 2000 y = 2004  (the addresses of x & y)

If you want:

x = 1000 y = 1004

Type,

printf("x = %p\ny = %p\n", x, y);

as is clear from the memory map above.




回答6:


In main, you are printing the adresses of the local variables a and b. In foo you are printing the addresses of the local variables x and y, not the addresses retained in them. You need to drop the & in foo if you want them to match.

Remember that a pointer is a variable that holds a memory address. You're confusing the address of the pointer variable with the address retained in the pointer. You need to print what the pointers hold if you want the two prints to match.




回答7:


In main, you are printing addresses of a & b variables..but in function foo(), you are printing addresses of pointers to x & y




回答8:


The first printf in main() displays the addresses of main's a and b.

The foo's printf displays the addresses of x and y.

What you want, I guess, is to display the addresses from main() of a and b

Change foo:

  printf("x = %p\ny = %p\n", x, y);

to display the addresses as they come from main()



来源:https://stackoverflow.com/questions/3815095/c-pointers-different-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!