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 ?
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