What happens when a casted pointer has an incremen

2019-02-04 15:18发布

问题:

For example:

int x[100];
void *p;

x[0] = 0x12345678;
x[1] = 0xfacecafe;
x[3] = 0xdeadbeef;

p = x;
((int *) p) ++ ;

printf("The value = 0x%08x", *(int*)p);

Compiling the above generates an lvalue required error on the line with the ++ operator.

回答1:

The cast creates a temporary pointer of type int *. You can't increment a temporary as it doesn't denote a place to store the result.

In C and C++ standardese, (int *)p is an rvalue, which roughly means an expression that can only occur on the right-hand side of an assignment.

p on the other hand is an lvalue, which means it can validly appear on the left-hand side of an assignment. Only lvalues can be incremented.



回答2:

The expression ((int *) p) treats the pointer stored inside the variable p is a pointer to int. If you want to treat the variable itself as a pointer to int variable (and then increment it), use a reference cast:

((int *&) p) ++ ;


回答3:

You can get the intended result with

p = (int*)p + 1;

Using the increment operator on a dereferenced pointer to p, which is an lvalue, also works:

(*(int**)&p)++;

However, the latter is not portable, since (void*)p might not have the same representation as (int*)p.



回答4:

Thanks to larsmans for pointing to the right direction.

I took the liberty of digging deeper into this. So for future reference, according to sections 6.5.2.4 and 6.5.4 of the C99 standard (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf):

6.5.2.4 Postfix increment and decrement operators

Constraints

The operand of the postfix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue
..
..
6.5.4 Cast operators
..
..
[Footnote] 89) A cast does not yield an lvalue. Thus, a cast to a qualified type has the same effect as a cast to the unqualified version of the type.

Note: This only applies to C. C++ may handle casts differently.



回答5:

Rvalue expression ((int *) p) creates and temporary of type int* on which operator ++ cannot be applied.

++ requires an lvalue as its operand.

As @FredOverflow mentions lvalues and rvalues have very little to do with assignment.

Arrays are lvalues still they cannot be assigned to because they are non-modifiable. std::string("Prasoon") is an rvalue expression still it can occur on the left side of assignment operator because we are allowed to call member functions( operator = in this case) on temporaries.