5

I have some legacy code which was usually compiled for PowerPC with GCC 3.4.4 . Now I am porting some code parts which I want to compile with the GCC 4.8.1 from MinGW. At some point in the code I found this:

// Prototypes
void foo(uint8* pData);
uint8 bar();

// Function
void foo(uint8* pData)
{
    (uint8) *(pData++) = bar(); // Original Code - Doesn't work with GCC 4.8.1
    *(pData++) = bar();         // Works with GCC 4.8.1
}

If I want to compile the line from the original code with the GCC 4.8.1 I get the lvalue required as left operand of assignment error. If I get rid of the cast operator it works fine. Can someone explain why this is? Isn't that cast just redundant and shouldn't matter anyway? And why is it working with the GCC 3.4.4 ?

Toby
  • 3,815
  • 14
  • 51
  • 67
  • What is the _purpose_ of this cast ? Even were you to use that statement as rvalue, the cast at the very least is still redundant / unnecessary. – FrankH. Oct 30 '13 at 09:09
  • @FrankH. It's legacy code - I didn't write this, so unfortunately I can't tell you what were the motivations for this line of code. – Toby Oct 30 '13 at 09:15

1 Answers1

6

The result of the cast operator is not an lvalue (you can think of it as a temporary that has the same value as the original object, but it has a different type -- it's just an unnamed value that you can't change), so you can't assign to it.

Edit: as to why this compiled with GCC 4.3: because that compiler is too permissive. Also, you didn't compile with warnings enabled, I assume. gcc -Wall issues the following diagnostic:

quirk.c: In function ‘main’:
quirk.c:8: warning: target of assignment not really an lvalue;
    this will be a hard error in the future
  • 3
    @Toby I don't know, maybe a bug, but **it most certainly should not be working.** –  Oct 30 '13 at 08:40
  • 1
    @Toby I can only *guess* that because of optimization `(uint8) *(pData++) = bar();` converted into => just `*(pData++) = bar();` that is a valid expression. In past I also encounter [similar bug in my gcc](http://stackoverflow.com/questions/14860189/expressions-j-i-i-and-j-i-i-should-be-a-lvalue-error) 4.4 compiler(and in older-version) And as I know from [comments to my question](http://stackoverflow.com/questions/14860189/expressions-j-i-i-and-j-i-i-should-be-a-lvalue-error#comment20830435_14860189) the bug was rectified @ GCC 4.6 – Grijesh Chauhan Oct 30 '13 at 14:16