开发者

address of array ptr equal to to it's value? [duplicate]

开发者 https://www.devze.com 2023-01-04 17:41 出处:网络
This question already 开发者_如何学Pythonhas answers here: Closed 12 years ago. Possible Duplicate:
This question already 开发者_如何学Pythonhas answers here: Closed 12 years ago.

Possible Duplicate:

C: How come an array’s address is equal to its value?

SA

In C I tried to print the address of the pointer of an array.

int a[3] = {0,1,2};
printf("\n%p",a);
printf("\n%p",(&a));

the 2 statement prints the same value why?

thanks in advance


Any array argument to a function will decay to a pointer to the first element of the array.

The C Book has an excellent explanation of this:

Another case is when an array name is the operand of the & address-of operator. Here, it is converted into the address of the whole array. What's the difference? Even if you think that addresses would be in some way ‘the same’, the critical difference is that they have different types. For an array of n elements of type T, then the address of the first element has type ‘pointer to T’; the address of the whole array has type ‘pointer to array of n elements of type T’; clearly very different


In C arrays "decay" to pointers to their first element in certain contexts. Passing an array to a function is one of these cases.


Because local arrays are treated as a special case (they decay to pointers).

In your case &a is translated to a.

If you try with an int pointer, e.g.,

int c = 3;    
int* p = &c;

you should get different values.


An array "decays" into a pointer to it's elements in some situations. Passing it as an argument to a function is one of those. Thus in

void func(int * pi, size_t nints);
int a[3] = {0, 1, 2};
func(a, 3);

the pointer to a is passed to func. This is what happens in your code as well. This also happens when you assign it to a pointer:

int * pi = a;
assert(*pi == 0); assert(*(pi + 1) == 1); assert(*(pi + 2) == 2);

The asserts here would also work if you replace pi with a.

0

精彩评论

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