R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215277

gcc confused about what's an lvalue?

gcc is giving me invalid lvalue in assignment errors for:

-2[(size_t *)new] = 0;

Changing the code to the following makes it go away:

((size_t *)new)[-2] = 0;

but as far as I can tell, both are 100% equivalent in C. gcc has no problem using the former in non-lvalue expressions. Is this just a bug in gcc? I've tested it with several versions and got the same results.

Upvotes: 2

Views: 347

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215277

I'm stupid. [] binds more tightly than -, so this expression is actually reading index 2 and negating it.

Upvotes: 7

Related Questions