开发者

C pointers - Different address

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


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)


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.


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


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.


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.


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.


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


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


In main, the address of variables a and b is printed. In function foo(), the dress of pointers x and y is printed, those are pointing to variables and b.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜