Answer the question
In order to leave comments, you need to log in
Why can't you increment an array name?
#include <stdio.h>
#include <math.h>
#define SIZE 8
int main(void)
{
int arr[SIZE];
for (int i = 0; i < SIZE; i++)
arr[i] = pow(2, i);
for (int i = SIZE - 1; i >= 0; i--)
printf("%d ", *(arr + i));
return 0;
}
*(arr++)
: int * arr = malloc(sizeof (int) * 8);
// some code
printf("%d", *(arr++)); // success
Answer the question
In order to leave comments, you need to log in
"Working Draft, Standard for Programming Language C++" (N4713)
#11.3.4 Note 7 (p.186) " Objects of array types cannot be modified "
Because it's pointless. Just like you can't increment an object of type std::vector. It doesn't matter where the memory is allocated, it matters what object you're trying to apply the operation to.
In the case of arrays, the increment does not make sense, because it is not clear what should happen to the array itself after that (after all, increment is an operation that modifies its argument, unlike, for example, the + operator).
You can increment an iterator/pointer to some element of this array to get an iterator/pointer to the next element.
It is important to understand that arrays are not pointers. These are fundamentally different entities.
An array contains all of its elements and spans its entire range (you can't force an array to span only part of its range after it's created).
A pointer only refers to memory owned by someone else (perhaps an array).
Although arrays can be implicitly cast to pointers, and in many cases this is done automatically.
For example, when applying the + operator to an array.
The + operator (in the general case) does not change its operands, but creates a new object, so there will be no such ambiguity with it.
In the expression arr+1 (where arr is an array), the compiler will first try to apply the + operator to an array and an integer, it will fail (because you can’t add something to an array itself - again, this is a pointless/ambiguous operation). But then it will see that this array has an implicit conversion operator to something that can be used in such a context: a pointer. And therefore, it will use this cast, convert arr to a new pointer, and apply +1 already to this pointer, as a result returning a pointer to the next element.
The fundamental difference is that in this example, no one is trying to modify the array itself.
You may ask: why then, in the case of ++arr, does the compiler not perform the same trick by casting the array to a pointer and applying an increment to this pointer?
The trick is that the array itself will not change from this (because only a temporary pointer object would change), and as a result you would not get anything. That's absolutely nothing. And this is hardly what you expect. Because this temporary incremented pointer would be immediately destroyed after the operation was completed - it would not affect the original array in any way. The operation was completely pointless.
Therefore, for built-in types, such operations on prvalue (temporary unnamed objects not bound to a variable) are prohibited. Here we are not talking specifically about arrays.
This is the same as writing ++(1+2). Will not compile. Since the expression (1 + 2) will return a temporary object of type int, not tied to any named variable, and it simply makes no sense to increment it.
So if you want to increment with an array, assuming that this operation should move to the next element - just create a named pointer variable through the implicit cast described above, and increment this pointer already.
In this case, everything will be fine, because 1) the given pointer will be a new object, and changing it will not in itself affect the array itself; 2) pointer increment - the operation is clear and unambiguous:
int arr[42];
//++arr; // error!
int* ptr = arr; // array-to-pointer decay
++ptr; // ok!
std::vector<int> arr(42);
//++arr; // error!
int* ptr = arr.data();
++ptr; // ok!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question