gpt4 book ai didi

c - 'const struct' 与 'struct' 有何不同?

转载 作者:太空狗 更新时间:2023-10-29 16:20:01 24 4
gpt4 key购买 nike

const struct 是什么意思?它与 struct 不同吗?

最佳答案

const 部分真正应用于变量,而不是结构本身。

例如@Andreas 正确地说:

const struct {
int x;
int y;
} foo = {10, 20};
foo.x = 5; //Error

但重要的是变量 foo 是常量,而不是 struct 定义本身。你同样可以这样写:

struct apoint {
int x;
int y;
};

const struct apoint foo = {10, 20};
foo.x = 5; // Error

struct apoint bar = {10, 20};
bar.x = 5; // Okay

关于c - 'const struct' 与 'struct' 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266914/

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