tags:

views:

69

answers:

2
#include<stdio.h>
int main(void) {
int arr[3]={1,2,3};
return 0;
}

Now what will *(&arr) give me and why? I want a detailed explanation. Don't just tell me how * and & cancel out :P

I want to know how the compiler interprets this expression to give the desired result.

+5  A: 

&arr creates a pointer to the array - it's of type int (*)[3], and points at the array arr.

*&arr dereferences that pointer - it's the array itself. Now, what happens now depends on what you do with it. If you use *&arr as the subject of either the sizeof or & operators, then it gives the size or address of the array respectively:

printf("%zu\n", sizeof *&arr);    /* Prints 3 * sizeof(int) */

However, if you use it in any other context, then it is evaluated as a pointer to its first element:

int *x = *&arr;
printf("%d\n", *x);    /* Prints 1 */

In other words: *&arr behaves exactly like arr, as you would expect.

caf
well, if i say printf(" %d ",*
n0nChun
It "creates it" in the same way that evaluating `2 + 3` "creates" the `int` value `5`.
caf
A: 

Since arr is a statically-allocated array, not a pointer variable - the expression &arr is equivalent to arr. Hence *(&arr) is actually *arr.

The thing would be different if arr was a pointer.

valdo
mohammad shamsi
caf