让我们从最后一行开始:
printf("%d",*(int*)((char*)na + (unsigned int ) & (((struct name *)NULL)->b)));
让我们解释一下:
(unsigned int ) & (( (struct name *)NULL)->b )
实际上是铸造& (( (struct name *)NULL)->b )成一个unsigned int.
& (( (struct name *)NULL)->b )是地址(即它给出了一个指针):
(( (struct name *)NULL)->b )
这实际上是b(as name.b) 从 NULL (0) 的偏移量,它是 4 个字节(假设 along是 4 个字节)并转换为 int 的指针,给你 2(假设int是 2 个字节)。
如果它不是NULL指向 的指针0xFFFF0000,那么&(ptr->b)将是0xFFFF0002。但它更像&(0 -> b)是它的0x00000002.
所以,(unsigned int ) & (( (struct name *)NULL)->b ) == 2(或者可能是 1,或者可能是 4,取决于机器)。
剩下的很简单:*(int*)((char*)na + 2将指向re->b. 所以它应该打印 4(在代码中已经初始化了什么r re ={3,4,5};)。
PS:即使 (unsigned int ) & (( (struct name *)NULL)->b ) != 2(可能是 1、4 或 8) - 它仍然应该打印 4 因为它使用相同的偏移量来获取值。