gibraltar
gibraltar

Reputation: 1708

Strange Behavior of Pointer of an array in C

Here is a code which I wrote to test/understand the behavior of pointers of/in an array

int main(void){
    int a[4];
    memset(a, 0, sizeof(a));
    printf("%x %x\n",a,&a);
}
Output of the above program on my machine:
bfeed3e8 bfeed3e8

I can't understand why are the values a and &a is same. From what I understand &a is supposed to give the address of memory location where a is stored. What is the explanation for this kind of behavior?

Upvotes: 4

Views: 212

Answers (8)

John Bode
John Bode

Reputation: 123448

The expression &a has type int (*)[4] (pointer to 4-element array of int) and evaluates to the address of the array object; in this respect, pointers to arrays are like pointers to anything else.

What's hinky is how the expression a is treated. Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to inialize another array in a declaration, an expression of type "N-element array of T" is replaced with / converted to / "decays" to an expression of type T * (pointer to T) whose value is the address of the first element in the array. Since the address of the array and the address of the first element in the array are the same, both expressions yield the same value, but their types are different.

Upvotes: 1

Israel Unterman
Israel Unterman

Reputation: 13510

In C, the array name, in this case a is considered a label, and represents the address of the array. Applying an ampesand to it is interpreted as the same thing.

There is a similar issue with function pointers. If func is pointer to a function, you may invoke the function using both func() and (*func)().

There are cases of indirection syntax, treated in a special way.

Upvotes: 0

iyasar
iyasar

Reputation: 1271

the array name "a" points the first element of array.

for better understand :

  *(a + 2) is same with a[2].

Upvotes: 0

TheTechGuy
TheTechGuy

Reputation: 17354

int test[10]         = test is pionter as well as &test

If you want to address an element other than base, use &test[4]

BTW

&test = @test[0] = test

all pointers to base element

Upvotes: 0

JeremyP
JeremyP

Reputation: 86651

In a pointer context, arrays decay to a pointer. So a decays to the address of the first byte of the array and &a is the address of the first byte by definition. Probably you wanted to use *a or a[0] instead of &a in your printf.

Upvotes: 0

karlphillip
karlphillip

Reputation: 93410

The name of an array also points to the start of the memory block it holds. No biggy.

These are all equivalent:

printf("%x\n",a);
printf("%x\n",&a);
printf("%x\n",&a[0]);

Upvotes: 0

Mr Lister
Mr Lister

Reputation: 46549

&a does give you the address of a. a gives you a, which, because it's an array, decays to a pointer to a.

Or to be pedantic, it decays to a pointer to a[0].

Upvotes: 9

Daniel Fischer
Daniel Fischer

Reputation: 183858

In that context, the array name a decays into a pointer to its first element. Did you mean

printf("%x %x\n",a[0],&a);

Upvotes: 4

Related Questions