c - getting error: cannot take the address of an rvalue of type 'int' -
i tryed compile old code new compiler , got next error:
error: cannot take address of rvalue of type 'int'
here example 2 lines - 1 compiles , other gives error
struct mstct { int myfield; int myfield2[5]; int myfield3[5]; }; typedef struct mstct db_data_px; int foo(int a, int b, int c){ //the next code compiles successfully. unsigned val1 = ((18 == c) ? ((unsigned) & (((db_data_px *) 0)->myfield)) : ((unsigned) & (((db_data_px *) 0)->myfield3[b]))); //successes //the next code failing unsigned val2 = (unsigned) & ((18 == c) ? (((db_data_px *) 0)->myfield) : (((db_data_px *) 0)->myfield3[b])); return 0; // failing }
why first line compiles , second failing ? why need cast (unsigned) & in both of select expression , not enough cast after select expression valued ?
in code
((18 == c) ? (((db_data_px *) 0)->myfield) : (((db_data_px *) 0)->myfield3[b]))
is conditional expression not produce lvalue.
the above expression gives rvalue (non-lvaue) , cannot use &
operator on that.
to elaborate, quoting c11
standard, chapter §6.5.3.2, address , indirection operators
the operand of unary
&
operator shall either function designator, result of[]
or unary*
operator, or lvalue designates object not bit-field , not declaredregister
storage-class specifier.
otoh, result type of conditional operator, chapter §6.5.15, footnote
a conditional expression not yield lvalue.
just imagine, &5
, not possible.
Comments
Post a Comment