【发布时间】:2010-01-09 16:00:06
【问题描述】:
使用的代码
#include<stdio.h>
struct st
{
char a;
short c;
int b;
};
struct st s1;
int main()
{
printf("%p %p \n",(&s1.b)-1, &s1);
}
如果我打印&s1.b 的地址,它会打印0x804a01c 和&s1.b-2 打印0x804a018
如果我选择&s1.b-1,为什么它会打印相同的地址0x804a01c?
【问题讨论】:
-
结构填充是实现定义的。
-
@Prasoon:与结构填充无关:
&s1.b是int*,结构中的填充对指针运算如何与int*一起使用没有影响。跨度> -
@Steve Jessop:没错。加一个你的评论。
-
@Steve:是的,你是对的。可能我在想别的东西。
-
我在 ubuntu 中使用 gcc