4

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?

1
  • Also, possible duplicate of Address of array Commented Apr 20, 2012 at 13:44

8 Answers 8

9

&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].

Sign up to request clarification or add additional context in comments.

6 Comments

That's what I said! No wait, that's not what I said, I only made it look like I said it. Sorry.
The difference between being pedantic and not being pedantic (which is the same as the difference between the result of a decaying, and &a) is the type. The array has the same address as its first element, but the type of &a is int(*)[4], whereas the type of &a[0] is int *
Then what is the type of whatever is pushed onto the stack if you specify a?
@MrLister Your stack is typed?
Still thinking of a retort, stand by.
|
4

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

Comments

1

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.

Comments

0

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

Comments

0

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.

Comments

0
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

Comments

0

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

for better understand :

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.