gpt4 book ai didi

const 结构声明

转载 作者:太空狗 更新时间:2023-10-29 15:15:14 26 4
gpt4 key购买 nike

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

struct S
{
uint8_t a;
};

const struct S
{
uint8_t a;
}

其次是:

void main(void)
{
struct S s = {1};
s.a++;
}

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

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

问候丰富

/********************************************/

我刚刚弄清楚了

const struct <typename> <{type}> <variable instances a, b, .., z>;

正在做:

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

const struct <typename> a, b, z;

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

丰富

最佳答案

结构声明只是定义了数据类型。

const 限定符适用于变量而非数据类型。因此,在结构声明之前添加 const 最多应该是多余的。

关于const 结构声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7647498/

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