tags:

views:

174

answers:

7

What's going in below isn't an arrayname always a pointer to the first element in C?

int myArray[10] = {0};

printf("%d\n", &myArray); /* prints memadress for first element */
printf("%d\n", myArray); /* this prints a memadress too, shows that the name is a pointer */

printf("%d\n",sizeof(myArray)); /* this prints size of the whole array, not a pointer anymore? */
printf("%d\n",sizeof(&myArray)); /* this prints the size of the pointer */
+3  A: 

No, an array is that first element (and the rest). It doesn't get converted into a pointer until you pass it as an argument to a function.

Carl Smotricz
+1  A: 

arrayname will point to all the elements of the array. That is the reason you can do (arrayname + 5) to point to the 5th element in the array.

aJ
It points to the first element, not to all.
Maximilian Mayerl
what I mean here is that arrayname points to entire object not any single element of the array.
aJ
A: 

An arrayname is not a pointer, but it can be treated as a pointer to the first element of the array.

Christian
+12  A: 

Array name is array name. Array name is an identifier that identifies the entire array object. It is not a pointer to anything.

When array name is used in an expression the array type gets automatically implicitly converted to pointer-to-element type in almost all contexts (this is often referred to as "array type decay"). The resultant pointer is a completely independent temporary rvalue. It has nothing to do with the array itself. It has nothing to do with the array name.

The two exceptions when the implicit conversion does not take place is: operator sizeof and unary operator & (address-of). This is exactly what you tested in your code.

AndreyT
But it isn't an identifier like with ints, doubles and so on for example you can't do like this: int myArray1[] = {0}; int myArray2[] = {0}; myArray2 = myArray1; - this would be like saying 1 = 3 ?
Chris_45
It is an identifier, it just can't be assigned to. Another identifier which can't be assigned to is this one: `const int a = 0;`, although not really for the same reason.
Steve Jessop
@Chris_45: It is identifier "like with ints", except that the assignment operator context is not excluded from the set of contexts where "array type decay" takes place. The result of that is that arrays are non-modifiable lvalues (as Steve said).
AndreyT
+3  A: 

An array is not a pointer. However, if an array name is used in an expression where it is not the subject of either the & operator or the sizeof operator, it will evaluate to a pointer to its first element.

caf
But how come this is true:int *point;point == myArray;point ==
Chris_45
caf
+2  A: 

Be wary of the types.

  • The type of myArray is int[10].
  • The type of &myArray is int (*)[10] (pointer to int[10]).
  • When evaluated, the type of myArray is int *. I.e. the type of the value of myArray is int *.
  • sizeof does not evaluate its argument. Hence sizeof(myArray) == sizeof(int[10]) != sizeof(int *).

Corollary:

  • myArray and &myArray are incompatible pointer types, and are not interchangeable.

You cannot correctly assign &myArray to a variable of type int *foo.

Alex
A: 

do arrayname++ and u will come to know that arrayname at a time represents the whole array and not only the starting element....by default it keeps the starting address of the first element

prakash