Joel B
Joel B

Reputation: 31

Error on defining an array even though its set via a Constant

I know this is really basic, but its got me stumped...

In Objective-C I'm trying to write:

const int BUF_SIZE = 3;

static char buffer[BUF_SIZE+1]; 

But I get a storage size of buffer isn't constant. How do I make Xcode realise that I'm setting it to a constant, + 1...? Or is this not possible...?

Thanks...!

Joel

Upvotes: 1

Views: 852

Answers (3)

MuffyDiver
MuffyDiver

Reputation: 11

Happens in gcc with things like:

#define LPBUFFER_LGTH ((int) (2*MS25))

as well. Workaround as above: hardcode the constant you want. I think the problem is with a 'define of a define' ie. twice.

Upvotes: 1

dreamlax
dreamlax

Reputation: 95335

You can use an enum:

enum
{
    BUF_SIZE = 3
};

Or a macro

#define BUF_SIZE 3

Upvotes: 1

hbw
hbw

Reputation: 15750

I think it's a C thing—if I recall correctly, C only allows you to specify array sizes with literal expressions (no symbols whatsoever). I'd just use a #define constant as a workaround.

Upvotes: 3

Related Questions