Variably modified array at file scope

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

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 }; 

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"

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:

  1. To have an array which is accessible throughout the file
  2. To encapsulate NUM_TYPES into a variable so I don't have the same literal scattered about different places in my file

Any suggestions?

[EDIT] Found this in the C Faq: http://c-faq.com/ansi/constasconst.html

回答1:

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.



回答2:

If you're going to use the preprocessor anyway, as per the other answers, then you can make the compiler determine the value of NUM_TYPES automagically:

#define NUM_TYPES (sizeof types / sizeof types[0]) static int types[] = {    1,   2,    3,    4 }; 


回答3:

#define NUM_TYPES 4 


回答4:

It is also possible to use enumeration.

typedef enum {     typeNo1 = 1,     typeNo2,     typeNo3,     typeNo4,     NumOfTypes = typeNo4 }  TypeOfSomething; 


回答5:

Imho this is a flaw in many c compilers. I know for a fact that the compilers i worked with do not store a "static const"variable at an adress but replace the use in the code by the very constant. This can be verified as you will get the same checksum for the produced code when you use a preprocessors #define directive and when you use a static const variable.

Either way you should use static const variables instead of #defines whenever possible as the static const is type safe.



回答6:

As it is already explained in other answers, const in C merely means that a variable is read-only. It is still a run-time value. However, you can use an enum as a real constant in C:

enum { NUM_TYPES = 4 }; static int types[NUM_TYPES] = {    1, 2, 3, 4 }; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!