Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiles as C++ but not C (error: lvalue required as unary '&' operand)

This line compiles when I use C++, but not C:

gmtime(&(*(time_t *)alloca(sizeof(time_t)) = time(NULL))); //make an lvalue with alloca

I'm surprised by this difference. There is not even a warning for C++.

When I specify gcc -x c, the message is:

playground.cpp:25:8: error: lvalue required as unary '&' operand
 gmtime(&(*(time_t *)alloca(sizeof(time_t)) = time(NULL)));
        ^

Isn't the & here just an address-of operator? Why is it different in C and C++?

Although I can use compound literals in C, still is it possible to modify my syntax to make it work in both C & C++?

like image 869
cshu Avatar asked Dec 30 '15 06:12

cshu


1 Answers

In C11 6.5.16/3:

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

In C++14 5.17/1:

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.

(Earlier versions of the language standards in each case specified the same thing).

Since the address-of operator can only operate on an lvalue, the code is correct in C++ but not in C.


Regarding the question "Is it possible to modify my syntax to make it work in both C & C++?". This is not a desirable goal; the two languages are different and you should decide what you're writing. This makes about as much sense as trying to stick to syntax that works in both C and Java.

As suggested by others, you could write:

time_t t = time(NULL);
gmtime(&t);

which has the benefits over your original code of being:

  • simpler, therefore easier to understand and maintain
  • does not depend on non-standard alloca function
  • does not have potential alignment violation
  • uses no more memory and perhaps uses less
like image 82
M.M Avatar answered Sep 30 '22 08:09

M.M