L-value to R-value conversion

211 views Asked by At

the site here says that: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc05lvalue.htm

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. 

Exceptions to this is:

    Situation before conversion             Resulting behavior

1) T is an incomplete type                  compile-time error
2) e refers to an uninitialized object      undefined behavior
3) e refers to an object not of type T      undefined behavior

Ques.1:

Consider following program,

int main()
{   
    char p[100]={0};      // p is lvalue
    const int c=34;       // c non modifiable lvalue

    &p; &c;               // no error fine beacuse & expects l-value
    p++;                  // error lvalue required  

    return 0;
}

My question is that why in expression (p++) ++(postfix) expects l-values and arrays are l-value then why this error occurs? gcc error: lvalue required as increment operand|

Ques.2:

Plzz explain the exception 3 with an example?

2

There are 2 answers

1
cnicutar On

Arrays are indeed lvalues, but they are not modifiable. The standard says:

6.3.2.1

A modifiable lvalue is an lvalue that does not have array type

0
nickie On

An answer to question 2.

Say you have an object of type double. You take a pointer and cast it to a different pointer type. Then you use the new pointer to dereference the object. This is undefined behavior.

double x = 42.0;
double *p = &x;
int *q = (int *) p;

*q;

Here, *q is an lvalue of type int which does not refer to an object of type int.