gpt4 book ai didi

c - C 中的运算符优先级和结构定义

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:44 24 4
gpt4 key购买 nike

struct struct0 {
int a;
};

struct struct1 {
struct struct0 structure0;
int b;
} rho;


&rho->structure0; /* Reference 1 */
(struct struct0 *)rho; /* Reference 2 */
(struct struct0)rho; /* Reference 3 */
  1. 引用文献1,编译器是获取rho的地址,然后访问structure0,还是相反?

  2. 引用 2 处的行是做什么的?

  3. 既然 structure0 是 struct1 的第一个成员,引用 3 是否等同于引用 1?

最佳答案

按照引用文献的顺序:

  1. -> 的优先级高于&。您将获得 rho->structure0 的地址。或者更确切地说,如果 rho 是一个指针,您会这样做。因为它不是,你会得到一个编译错误。
  2. 那是行不通的 - 您正在将结构转换为指针类型 - 您这样做应该会遇到编译器错误。
  3. 你也不能那样做。类型转换为非标量类型也是错误。

您的示例 #2 和 #3 包含在标准第 6.5.4 节中:

Unless the type name specifies a void type, the type name shall specify qualified or unqualified scalar type and the operand shall have scalar type.

如果将任何代码放入编译器中,您会看到相同的结果;您显示的代码不是您要询问的代码吗?

关于c - C 中的运算符优先级和结构定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2459393/

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