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

标签 c++ c compilation

在这样的定义中

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/

相关文章:

c++ - 如何制定时间条件?

c++ - 派生为模板参数的基本转换

c++ - 创建 "relative"优先级队列的算法? (c/c++)

c - 为什么这个 fgets() 循环永远不会结束?

Java 编译器不创建 .jar 文件

python - __init__.py 代码被调用两次及其对包导入的意义

java - 如何在eclipse中从编译中排除文件夹

c++ - 从 DXVA2 渲染 IDirect3DSurface9?

c - ZeroMQ - 4.x 和 2.2 与 LogStash 之间的向后兼容性

c++ - 那么为什么 i =++i + 1 在 C++11 中定义良好?