Working on a project that must run in Visual C++ and GCC
Before understanding that lvalue casting of the assignment operator in C is not allowed, I was writing code like this in VC++:
typedef uint64_t QWORD;
QWORD A = 0xdeadbeef, T = 0;
(char)A = T;
Notice the (char)A = T disassembles to:
000000013F7F3D8E movzx eax,byte ptr [T]
000000013F7F3D95 mov byte ptr [A],al
and everything was working fine (it seemed). Next, I ran this code using GCC on linux and it reported this error:
"error: lvalue required as left operand of assignment"
After searching many Stack Overflow articles, I found a solution that works in VC++ and GCC:
*(char*)&A= T;
and also correctly disassembles to
000000013F7F3D9B movzx eax,byte ptr [T]
000000013F7F3DA2 mov byte ptr [A],al
But, why did VC++ permit me to cast the lvalue?
User contributions licensed under CC BY-SA 3.0