Address of the first element in a static array declaration

int main() { int a[3]={1,10,20}; printf("%u %u %u \n" ,&a,a,&a[0]); return 0; } 

This prints the same value for all three. I understand that a and & a [0] are the same, but how is & a the same?

+7
source share
5 answers

For maximum compatibility, you should always use %p and explicitly use void* to print pointer values โ€‹โ€‹using printf .

When an array name is used in an expression context other than the sizeof or unary & operand, it splits into a pointer to its first element.

This means that a and &a[0] are of the same type ( int* ) and value. &a is the address of the array itself, therefore it is of type int (*)[3] . An array object begins with its first element, so the address of the first element of the array will have the same value as the address of the array itself, although the expressions &a[0] and &a are of different types.

+14
source

My C is rusty, but as far as I know, & a is the address of the beginning of the array, which will exactly match & a [0] since the beginning of the IS array is the first element.

Again, rusty with C, so I will give it to the future specialist.

+6
source

The fact that & a turns out to be the same as a in this case is a consequence of the fact that a is statically sized. If you pointed to an array, then it will be a different number, while the other two will give you the same result.

For example:

 int main() { int b[3]={1,10,20}; int* a = b; printf("%u %u %u \n" ,&a,a,&a[0]); return 0; } 

Output Example:

2849911432 2849911408 2849911408

+4
source

&a is the "array address". This is the place in memory where the array is located, but it has a special pointer to the array from.

&a[0] is the "address of the 0th element of the array". Since there is nothing in the element until the 0th element, this is the same place in memory, but with the type "pointer to".

a is an "array". But you cannot pass arrays by value in functions (e.g. printf ) in C or C ++. You can only pretend to do this. (In C ++, you can pass arrays by reference, but C has no gaps). But what really happens is that the array "decays" into a pointer to the first element, just as if you wrote &a[0] .

This happened because (mainly I reflect here) in C, it was considered useful and desirable: (a) to be able to modify the contents of an array of transmitted ones; (b) no need to copy entire arrays onto the stack when calling a function. That way, C lies with you and instead pushes the pointer to the original array on the stack. Since the syntax is the same, you never notice the difference - when you write a[0] with what you think is an array, but is actually a pointer, it accesses pointed memory as if it were an array ( no matter what it is - thereโ€™s no longer any way to tell). In C ++, behavior is maintained for backward compatibility.

+3
source

In C, an array name points to the very first element of this array.

So, by specifying &a , you are referencing the memory address of this first element.

+1
source

All Articles