Cast-related invalid lalue value in unary &
I am getting an invalid lvalue error using gcc 3.4.6. The line causing the error has a function that receives a pointer to SetElement (which is typedef void*
). I am trying to achieve this, using (as the cast of another type typedef void*
), as follows: &((SetElement)my_var)
. gcc complains about this. Any idea what can be done?
source to share
You are taking the address of a temporary (a rvalue
) which cannot be executed. This looks like trying:
&(2+2)
// or
&4
// or
&(my_ptr + 4)
You can create a temporary user (thus lvalue
) using one of two methods:
AnotherType **my_var_ptr = &my_var;
SetElement **set_element_ptr = (SetElement *)my_var_ptr;
// or
SetElement *set_element = (SetElement)my_var;
SetElement **set_element_ptr = &set_element;
Or you can just differentiate between the other ( solution ):
SetElement **set_element_ptr = (SetElement *)&my_var;
This works because you are taking address my_var
(a lvalue
) and not (SetElement)my_var
(a rvalue
).
source to share