C语言编程(结构体)

标签 c

我有一个问题。假设我们有一个定义如下的结构:

 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/

相关文章:

将十进制转换为十六进制和八进制

c++ - 将所有 nsdata 字节复制到 char* 错误中

c链表从txt文件读取

c++ - 如何使用 libgeotiff 将坐标写入 TIF 文件

c - shell如何扩展*.c?

c - 捕获信号时使 open() 返回

c - 我怎么知道用户输入的字符是否超过 1 个?如果他输入了多个字符,我想将其存储在字符数组中

c - scanf 之后 fgets 不起作用

c - 我想在不使用终端的情况下运行程序 - 这可能吗?如何?

c - 如何在 C 中将数据包写入 TAP 接口(interface)?