Why isn’t this pointer arithmetic allowed in C? [duplicate]

arr + 1 is indeed a pointer to the second element of the array (i.e. &arr[1]).

However, that does not mean that you can somehow write that pointer value back into arr. You can’t do it for at least two reasons.

Firstly, arr is an array of char elements, not a pointer. There’s an obvious type mismatch here.

Secondly, being an array, arr a non-modifiable lvalue. You cannot change arr itself, you can only change its elements (this distinction is somewhat hard to grasp, but it is there).

Finally, if we just ignore the deeper intricacies and focus on what formally happens at the top level, due to array type decay your expression is equivalent to

(char *) arr = (char *) arr + 1;

The assignment is impossible since the left-hand side is a result of [implicit] type conversion. In C type conversions always produce rvalues. You cannot assign to rvalues.

In other words, it is not the “pointer arithmetic” that’s disallowed here. The pointer arithmetic is fine. It is what you do with the result of that pointer arithmetic that causes the error.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)