【问题标题】:What is the type of b in this code snippet?此代码片段中的 b 类型是什么?
【发布时间】:2017-02-06 13:49:51
【问题描述】:
typedef struct node {
    int value;
    struct node *next;
} NodeT;
const NodeT *a,b,c;

变量b实际上是一个常量结构元素吗?

【问题讨论】:

  • 不,不是。它是const struct node 变量,不是struct 元素,也不是常量。 const 并不意味着它是常量,而是告诉编译器你在初始化后永远不会改变它。

标签: c struct typedef


【解决方案1】:

变量b实际上是一个常量结构元素吗?

您的b 具有const NoteT 类型,这意味着const struct node,作为声明:

const NoteT *a,b,c;

相当于:

const NoteT *a;
const NoteT b;
const NoteT c;

一般来说,如果你有这样的声明:

type *x, y, z, *w, ... ;

type 类型适用于所有变量,但只有前面有* 的才是指向该类型的指针。

【讨论】:

  • OP 要求“常数”。 const 并不意味着“恒定”!而NoteT只是一个别名,所以实际的类型是const struct node
  • @Olaf 这适用于我的答案吗?
  • 它在您编辑之前发生,正如您写的“是的,它是”。这是错误的。我的其余评论仍然适用。
  • 补充一点:*[] 一样,是语法的不同部分(类型说明符的一部分),而不是限定符和存储类说明符。这就是int* p; 误导读者的原因。
【解决方案2】:

b 具有完全限定类型 const struct nodeconst 限定符适用,但指针不适用。

【讨论】:

  • 类型为const struct nodeNodeT 只是一个别名。
  • @Olaf,你又是对的。精度很重要。我会删除这个。
  • 其实我改改了。另一个答案掩盖了这一点。
  • @Olaf,类型是struct nodeconst是一个限定符:)
  • @KeineLust const 是一个类型限定符,它限定它作为问题类型的一部分;-)。假设我只是忘了写“完全限定类型”。
【解决方案3】:
const NodeT *a,b,c;

向后阅读,因为你只对b感兴趣

const NodeT b;

bconst NodeT

NodeTstruct node

因此bconst struct node

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2023-01-09
    • 2021-03-10
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多