文件范围内可变修改的数组 [英] Variably modified array at file scope
问题描述
我想在我的 Objective-C 实现文件中创建一个常量静态数组,类似于我的.m"文件的顶层:
I want to create a constant static array to be used throughout my Objective-C implementation file similar to something like this at the top level of my ".m" file:
static const int NUM_TYPES = 4;
static int types[NUM_TYPES] = {
1,
2,
3,
4 };
我计划稍后在文件中使用 NUM_TYPES
,因此我想将其放入一个变量中.
I plan on using NUM_TYPES
later on in the file so I wanted to put it in a variable.
但是,当我这样做时,我收到错误
However, when I do this, I get the error
在文件范围内可变修改的‘类型’"
"Variably modified 'types' at file scope"
我认为这可能与数组大小是一个变量有关(当我在那里放置一个整数文字时,我没有收到此消息,例如 static int types[4]
).
I gather that this may have something to do with the array size being a variable (I don't get this message when I put an integer literal there, like static int types[4]
).
我想解决这个问题,但也许我做错了……我在这里有两个目标:
I want to fix this, but maybe I am going about it all wrong...I have 2 goals here:
- 拥有一个可在整个文件中访问的数组
- 将
NUM_TYPES
封装到一个变量中,这样我的文件中的不同位置就不会散布相同的文字
- To have an array which is accessible throughout the file
- To encapsulate
NUM_TYPES
into a variable so I don't have the same literal scattered about different places in my file
有什么建议吗?
在 C 常见问题解答中找到了这个:http://c-faq.com/ansi/constasconst.html
Found this in the C Faq: http://c-faq.com/ansi/constasconst.html
推荐答案
出现这个警告的原因是 c 中的常量并不意味着常量.它的意思是只读".因此该值存储在内存地址中,并且可能会被机器代码更改.
The reason for this warning is that const in c doesn't mean constant. It means "read only". So the value is stored at a memory address and could potentially be changed by machine code.
这篇关于文件范围内可变修改的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!