我有一个问题。假设我们有一个定义如下的结构:
typedef struct test {
int x;
int y;
};
现在,如果我按如下方式创建此结构的实例;
test object;
“&object == &object.x”也会这样吗??
最佳答案
是的,结构体的第一个数据成员的地址将等于结构体本身的对象的地址。
来自 C 标准(6.7.2.1 结构和 union 说明符)
15 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
关于C语言编程(结构体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849116/