I don’t know how to express my problem. The code is below, please tell me why the value of a got changed? In my opinion, the GET macro just returns the value of a, NOT the address of a…
#include <stdio.h>
#define GET(addr) (*(int *)(&addr))
int main()
{
int a = 10;
GET(a) = 20;
printf("%d\n", a);
}
A result of dereferencing a pointer is an L-value, because it has an address; it certainly can be assigned.
Consider a simpler example: if
pis an array or a pointer, you can certainly assign top[0], as inNow recall that
p[0]is the same as*p. Now expand your macro:This is equivalent to
which is certainly a valid (albeit somewhat unorthodox) assignment of the
aitself.