Fuad
Fuad

Reputation: 1509

C: Ampersand operator applied to an array vs pointer

Let's say we have

int foo[4] = {1, 2, 3, 4};

foo would then be a pointer to the first element of the array. We can do:

printf("%p", foo); // OUTPUT is some address 0xffffcc00

Now if we do:

printf("%p", &foo); // OUTPUT is the same address 0xffffcc00

Looking online, I see this particular syntax for &<array-name> takes the address of the entire array. This explains the two same values above, because the starting element's address is the same as the whole array's address.

But in general, my understanding is that & should take address of whatever is on its right. (i.e. in this case, it "should" have taken the address of the pointer to the first element of the array.)

So why isn't &foo taking the address of the pointer foo? If this is an exception to the C language, is this the only exception, or are there other cases like this?

Upvotes: 3

Views: 1369

Answers (3)

storenth
storenth

Reputation: 1225

I prefer using this explanation about '& basically means "the address-of operator"' thesis. So, Arrays are not pointers! But an array can automatically "decay" into a pointer. Into &foo[0], in your case. But &foo is a pointer to an array of four integers because of its type. To get PoC you need to check ptype foo vs ptype &foo under GDB.

Upvotes: 0

VJAYSLN
VJAYSLN

Reputation: 473

Basically, Pointers used to access array elements in a alternative ease way.In Low level Programming, each and every variable has an address to allocate in memory. BTW, the array is also a variable name as like scalar variable.

Unlike Scalar Variables, Array variable has few things.,

  • List(sequences) of elements allocated in subsequent memory address.
  • So we can get all the elements by using first element address

FYI, Array Name == Address of the first element == Address of the array

(foo == &foo == &foo[0])

So while you are trying to get elements by using this notation foo, initially it points to (&foo + 0) , so based on ith value you can access all the elements one by one. Hope this Answer will helps.:)

Upvotes: 0

dbush
dbush

Reputation: 223872

There is a common misconception that pointers and arrays are the same thing. They are not, but they are related.

Your first example works:

printf("%p", foo);

Because in most contexts an array decays to a pointer to the first element of the array.

One of the situations where this is not the case is when an array is the operand of the address-of operator &. This gives the address of the entire array, not just the first element (even though the values are the same).

This is detailed in section 6.3.2.1p3 of the C standard:

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

Upvotes: 4

Related Questions