18

I have a question about the array name a

int a[10]

How is the array name defined in C++? A constant pointer? It is defined like this or just we can look it like this? What operations can be applied on the name?

2 Answers 2

34

The C++ standard defines what an array is and its behaviour. Take a look in the index. It's not a pointer, const or otherwise, and it's not anything else, it's an array.

To see a difference:

int a[10];
int *const b = a;

std::cout << sizeof(a); // prints "40" on my machine.
std::cout << sizeof(b); // prints "4" on my machine.

Clearly a and b are not the same type, since they have different sizes.

In most contexts, an array name "decays" to a pointer to its own first element. You can think of this as an automatic conversion. The result is an rvalue, meaning that it's "just" a pointer value, and can't be assigned to, similar to when a function name decays to a function pointer. Doesn't mean it's "const" as such, but it's not assignable.

So an array "is" a pointer much like a function "is" a function pointer, or a long "is" an int. That is to say, it isn't really, but you can use it as one in most contexts thanks to the conversion.

Sign up to request clarification or add additional context in comments.

2 Comments

*a would be printing the value of the 1st element in the array and so supposed to point to the memory address of the 1st element in the array. So why is the sizeof operator giving the size of all the elements of array a instead of giving the size of only storing the memory address of the 1st element of the array?
@user1825567: because the expression *a has an implicit conversion in it (called a "decay"), from array type to pointer type. You could think of it as a shorthand for *pointer_to_first_element_of(a). It should then be fairly obvious why sizeof(a) need not be the same as sizeof(pointer_to_first_element_of(a)). a is not a pointer. Anyone who tells you it is either doesn't understand C or doesn't respect you enough to think you understand C.
6

An array name is not a constant pointer - however it acts like one in so many contexts (it converts to one on sight pretty much) that for most purposes it is.

From 6.3.2.1/3 "Other operands/Lvalues, arrays,and function designators":

Except when it is the operand of the sizeof 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.

1 Comment

I want to note that this is a quote out of the C Standard. In C++, the array is not decayed regardless. It takes the context into account, for example: int a[10]; int(&r)[10] = a; /* no decay */;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.