views:

3416

answers:

4

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?

+7  A: 

What happens if you do it as a define instead? #define kNUM_TYPES 4 ?

Jorge Israel Peña
That works...for some reason I was trying to stay away from using the preprocessor because I thought I remembered reading that somewhere, but I just did some more research and couldn't find a good reason not to use it in this case. I think it may be less desirable if I'm creating objects in the preprocessor (like `@"An NSString literal"`)The only thing wrong with your piece of code is that there's no need for the semicolon.
Sam
Ah yes, thanks for the heads up, and glad I could help.
Jorge Israel Peña
+4  A: 
#define NUM_TYPES 4
Jim Buck
+3  A: 

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 };
caf
Wow that's really cool...I did not know that was possible. I assume the cost of this computation is negligible. Might I also assume that a compiler could optimize this to a static value?
Sam
Yes, the result of `sizeof` on objects like that is a compile-time constant.
caf
+3  A: 

It is also possible to use enumeration.

typedef enum {
    typeNo1 = 1,
    typeNo2,
    typeNo3,
    typeNo4,
    NumOfTypes = typeNo4
}  TypeOfSomething;
Dave L Delaney
That would work too, thanks.
Sam