小编Ric*_*urs的帖子

const 结构声明

有人能告诉我结构声明的这两个版本之间的区别吗?

struct S
{
  uint8_t a;
};
Run Code Online (Sandbox Code Playgroud)

const struct S
{
  uint8_t a;
}
Run Code Online (Sandbox Code Playgroud)

其次是:

void main(void)
{
  struct S s = {1};
  s.a++;
}
Run Code Online (Sandbox Code Playgroud)

提示,我已经在 Visual Studio C++ 2010 Express 中尝试了 S 的两个版本,所以我知道两者都编译出错。

“const struct”什么都不做?"const struct S s = {1};" 当然可以,但这不是目前的问题。

问候丰富

/********************************************/
Run Code Online (Sandbox Code Playgroud)

我刚刚弄清楚了什么

const struct <typename> <{type}> <variable instances a, b, .., z>;
Run Code Online (Sandbox Code Playgroud)

是在做:

当 const 出现在“struct”之前时,所有变量实例都是 const,就好像它们被定义为:

const struct <typename> a, b, z;
Run Code Online (Sandbox Code Playgroud)

所以它确实做了一些事情,但当没有与结构声明一致的实例定义时就不会。

富有的

c struct constants declaration

4
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×1

constants ×1

declaration ×1

struct ×1