常量对象的常量数组
如何在 C(而不是 C++)中定义常量对象的常量数组?
我可以定义
int const Array [] = {
/* init data here */
};
,但这是一个常量对象的非常量数组。
我可以使用
int const * const Array = {
/* init data here */
};
并且它可能会起作用。 但是可以使用数组语法来做到这一点吗? (这样看起来更干净)
How do you define constant array of constant objects in C (not C++)?
I can define
int const Array [] = {
/* init data here */
};
but that is a non-constant array of constant objects.
I could use
int const * const Array = {
/* init data here */
};
and it would probably work.
But is it possible do this with array syntax? (Which looks more clean)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数组不能是“常量”——这是什么意思?无论如何,数组大小已经是编译时常量,如果所有成员都是常量,那你还想要什么呢?您试图排除 const int[] 可能发生哪种突变?
An array cannot be "constant" -- what is that even supposed to mean? The array size is already a compile-time constant in any case, and if all the members are constants, then what else do you want? What sort of mutation are you trying to rule out that is possible for a
const int[]
?“双重常量”仅适用于指针,因为它们可以更改为指向其他内容1,因为数组的特征本身是静态的(数组的大小/类型/指向的内容不能更改)到其他东西)唯一可以应用的
const
是它们的元素。int
的指针”、“指向常量int
的指针”、“指向int
的常量指针”、“指向常量int
的常量指针”。The "double constness" thing applies only to pointers because they can be changed to point to something else1, since the characteristics of arrays are statical by themselves (arrays cannot be changed in size/type/to point to something else) the only
const
you can apply is to their elements.int
", "pointer to a constantint
", "constant pointer to anint
", "constant pointer to a constantint
".如果你希望数组的元素不被修改,只需使用:
const int Array[];
If you want the elements of array do not modify, just use this:
const int Array[];