6

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 */
6
  • 2
    In order to printf pointer values you should use %p format specifier. You are using %d, which is completely meaningless thing to do. Commented Dec 4, 2009 at 8:34
  • The C FAQ seems to have gone down, but you Google "c faq arrays" and access the cached edition.
    – Artelius
    Commented Dec 4, 2009 at 8:47
  • @AndreyT what do you mean meaningless, you get it in decimal with %d and hexadecimal with %p, right, but you're point may be memory is in hexa by def?
    – Chris_45
    Commented Dec 4, 2009 at 8:50
  • 2
    A pointer is a different datatype from an integer (or long) and may have internal structure that's not suitable for displaying with an integer format. Example: In Turbo C on 386, pointers were segment:offset (i.e. 2 integers) and had special formatting with %p; outputting with %d would have either failed or hidden some information. Commented Dec 4, 2009 at 9:13
  • And on x86_64 %p wants an 8 byte data type, while %d is only 4 bytes. Commented Dec 4, 2009 at 9:54

7 Answers 7

18

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.

3
  • 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
    Commented Dec 4, 2009 at 8:58
  • 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. Commented Dec 4, 2009 at 12:08
  • @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). Commented Dec 4, 2009 at 14:27
5

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.

4

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.

2
  • But how come this is true: int *point; point == myArray; point == &myArray[0];
    – Chris_45
    Commented Dec 4, 2009 at 9:10
  • Because in both those cases, myArray is not the subject of either & or sizeof, so it evaluates to a pointer to its first element - that is, &myArray[0].
    – caf
    Commented Dec 4, 2009 at 10:36
3

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.

1

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.

1
  • what I mean here is that arrayname points to entire object not any single element of the array.
    – aJ.
    Commented Dec 4, 2009 at 8:51
0

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

0

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.