如何做一个“常量结构'从'结构'有什么不同? [英] How does a 'const struct' differ from a 'struct'?

查看:107
本文介绍了如何做一个“常量结构'从'结构'有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么 const的结构是什么意思?它是从结构有什么不同?

What does const struct mean? Is it different from struct?

推荐答案

常量部分真的适用于变量,而不是结构本身。

The const part really applies to the variable, not the structure itself.

例如。 @Andreas正确说:

e.g. @Andreas correctly says:

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

但重要的是,变量是恒定的,而不是结构定义本身。
你同样可以写为:

But the important thing is that variable foo is constant, not the struct definition itself. You could equally write that as:

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

这篇关于如何做一个“常量结构'从'结构'有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆