Possible Duplicate:
Is array name a pointer in C?
I was running the following code
#include <stdio.h>
int main()
{
int a[4] = {1,2,3,4};
int (*b)[4] = &a;
int k = sizeof(a);
printf("\n\n%d\n\n", k);
printf("a = %u, b = %u, *b = %u, data = %d", a, b, *b, **b);
return 0;
}
I got the following output
a = 3485401628, b = 3485401628, *b = 3485401628, data = 1
Here I am assigning the address of a to b, as the type of b is int**, but in the output I am getting that the address pointed by the a is the same as the address pointed by b.
It seems little confusing to me. What is the explanation?
The value of array
ais the pointer to the first element of the arraya.The value of pointer
&a(which is the same value and type as pointerb) is the pointer to the arraya.They have the same value (but different types) as they both start at the same memory address. There is no padding possible at the beginning of an array.
When evaluated
ais of typeint *and&ais of typeint (*)[4].Note that the correct way to print the address of a pointer is to use conversion specifier
p. E.g.,