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 two goals here:
- 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
What can I do?
I found this in the C FAQ (11.8): I don't understand why I can't use const values in initializers and array dimensions
#define kNUM_TYPES 4
? – Bontebok@"An NSString literal"
) The only thing wrong with your piece of code is that there's no need for the semicolon. – Photofinishing