Reputation: 179
If an lvalue appears in a situation in which the compiler expects an rvalue, the compiler converts the lvalue to an rvalue.
An lvalue e of a type T can be converted to an rvalue if T is not a function or array type. The type of e after conversion will be T.
can someone tell when compiler expects Rvalue .or what is exaclty rules such that an expression reducuing to Lvalue is Converted into Rvalue. in C when we try to declare variable size array
int b=8;
int a[2*b]; //compiler gives error that constant expression reqd. what is this constant expression (is it rvalue expression)
but when i do
int a[10];
a[2*b]=89;
it gives no error plz someone elaborate when Lvalue to Rvalue Conversion Happens?? my Confusion is that in array subscript in first case it Lvalue to Rvalue convesion not happens (at declaration time) but in second case it happens
Upvotes: 0
Views: 187
Reputation: 11047
an lvalue is a value has an identity (name) and not movable. and rvalue is a value not a lvalue.
So there are two concepts (is it has a name? is it movable?) that are used to define what an lvalue is and what a rvalue is.
pure rvalue (prvalue) has no name and movable. There are also xvalue and glvalue related to this concept.
The C++11 standard for lvalue and rvalue conversion can be found at chapter 4.1 (page 85 for version 3485). You can also read Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?
Upvotes: 0
Reputation: 158629
It seems like you are possibly using an old compiler or maybe Variable length array(VLA
) support needs to be enabled? VLA
is supported since c99
, if you are using gcc
you can see this more clearly, compiling this sample code:
int main()
{
int b=8;
int a[2*b];
return 0 ;
}
with the following options:
gcc -std=c89 -pedantic
you will see this error:
warning: ISO C90 forbids variable length array ‘y’ [-Wvla]
if you use c99
though it is fine:
gcc -std=c99 -pedantic
but it is important to note that as of 2011 C standard VLA
support is now optional.
An lvalue
is an object that has a memory location while an rvalue
is a temporary value that does not persist usually beyond the end of an expression. This article Understanding lvalues and rvalues in C and C++ probably is one of the better detailed explanations.
In this case 2*b
is an rvalue
since it does not persist beyond the expression.
Upvotes: 1