gpt4 book ai didi

c++ - 在新结构中声明的数组的地址

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:25 24 4
gpt4 key购买 nike

在这样的定义中

typedef struct 
{
myType array[N];
} myStruct;

myStruct obj;

我总是可以假设([edit] 假设会发生正确的转换,这不是这里问题的重点 [/edit])

(&obj == &obj.array[0])

将返回 TRUE,或者我是否应该担心编译器引入额外的填充以适应 myType 对齐要求?理论上这不应该发生,因为该结构只有一个字段,但我对此并不完全确定。

最佳答案

通过适当的转换,这将始终返回 true。

来自 C standard 的第 6.7.2.1 节:

13. 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/37005295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com