Take the 2-minute tour ×

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.

share|improve this question

5 Answers 5

up vote 18 down vote accepted

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.

share|improve this answer
In C++, lvalue and rvalue have very little to do with assignments. array_name is an lvalue that cannot occur on the left-hand side of an assignment, and std::string("hello") is an rvalue which can. –  FredOverflow Mar 20 '11 at 8:11
@FredOverflow : Yes correct. BTW I added an answer addressing those issues. :) –  Prasoon Saurav Mar 20 '11 at 8:21

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) ++ ;
share|improve this answer

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:


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

share|improve this answer

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 and 6.5.4 of the C99 standard (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf): Postfix increment and decrement operators


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.

share|improve this answer

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.

share|improve this answer
There are no scalar temporaries :) The result of evaluating the rvalue ((int *) p) is just a value. –  FredOverflow Mar 20 '11 at 8:28
@FredOverflow : I don't think the statement "There are no scalar temporaries" is 100% correct. What do you have to say about const int& ref = int()? Doesn't it create a scalar temporary? –  Prasoon Saurav Mar 20 '11 at 9:27
int() is the same as 0, but you have a point: binding to a reference-to-const can implicitly create a scalar temporary. How about this: "There is no expression whose result is a scalar temporary." –  FredOverflow Mar 20 '11 at 10:15
@FredOverflow : const int& ref = int() as a whole is an expression, right? It can create a scalar temporary. So I've got problems with your second statement "There is no...." as well. :-) –  Prasoon Saurav Mar 20 '11 at 10:18
@FredOverflow : Does the Standard explicitly say "you can't have a scalar temporary" ? I think no. –  Prasoon Saurav Mar 20 '11 at 10:19

Your Answer


