Address of first element in static declaration of array
Asked Answered
L

6

12
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] is same but how is &a also same?

Loriloria answered 24/6, 2011 at 19:13 Comment(0)
W
22

For maximum compatibility you should always use %p and explicitly cast to void* to print pointer values with printf.

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

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

Wilcher answered 24/6, 2011 at 19:20 Comment(1)
This is what I would've said if I was more comfortable with C. Bam.Draggle
D
6

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

Again, rusty with C so I'll defer to someone with better expertise.

Draggle answered 24/6, 2011 at 19:17 Comment(3)
Hah, thanks! Now that I'm actually in the industry I'm starting to care a tiiiiiny bit more about my reputation.Draggle
Wait, &a is a pointer to the array, which therefore is a pointer to an array of 3 ints. This is not the same as a pointer to an int in the array, right?Inness
@Siidheesh Correct. Each int would exist as a primitive in the array. Going abstract, &a is the physical location of a cardboard box, and the 3 ints inside are like blocks...you describe them as being contained within the box, you do not describe them as BEING the box. Subtle difference, but you got it :-)Draggle
S
5

&a is "the address of the array". This is the location in memory where the array is, but it has a special type "pointer to array of ".

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

a is "the array". But you can't really pass arrays by value to functions (such as printf) in C or C++. You can only pretend to do so. (In C++, you can pass arrays by reference, but C has no pass-by-reference.) But what really happens is that the array "decays" into a pointer to the first element, just as if you had written &a[0].

This happened because (I am mostly speculating here) in C, it was considered useful and desirable to (a) be able to modify the contents of a passed-in array; (b) not have to copy entire arrays onto the stack when calling a function. So C lies to you and puts a pointer to the original array onto the stack instead. Since the syntax is the same, you never really notice the difference - when you write a[0] with what you think is an array but is really a pointer, it accesses the pointed-at memory as if it were an array (whether or not it actually is - there is no longer a way to tell). In C++, the behaviour is preserved for backwards compatibility.

Sestina answered 24/6, 2011 at 20:14 Comment(0)
Y
4

The fact that &a happens to be the same as a in this case is a consequence of the fact that a is statically sized. If you made a a pointer to an array, then it would be a different number while the other two gave 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;
}

An example output is:

2849911432 2849911408 2849911408

Yand answered 24/6, 2011 at 19:17 Comment(0)
S
1

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

So by stating &a you are referencing the memory address of that first element.

Staffman answered 24/6, 2011 at 19:20 Comment(0)
T
0

It is confusing because of the oversimplification that say (ArrayName is pointer), actually compiler doesn't deal with array names this way and in fact (Array indexing = Pointer arthimetics).

Applying this to your code &a[0] >> it is understandable that this is returning the address of the first element in the array which is not confusing

&a >> According to our understand of pointers, we expect it will print any arbitary memeory location that stores the address of the first element of the array, and ofcourse it must be unique !?

Actually this hypotheses is NOT true, because simply the compiler will deal with &a the same way it deals with a and will generate assembly code to do the same task and return the same location. This is one of the core diffrences between Arrays and Pointers.

Also, it makes perfect sense that &a = a, because a is a constant pointer in nature that points to a fixed address and there is no reason you make another pointer to point at it rather than pointing directly to the thing that constant pointer ( a ) points at, which in this case the first element in the array.

Also, there is a catch here.

&a[0] >> emits a pointer of the following type pointer to integer

&a and a>> emits a pointer of the following type pointer to array of integers So, both points to the same loction, however they have different C types !

Trigonal answered 1/3, 2023 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.