Flavian Hautbois
Flavian Hautbois

Reputation: 3060

Errors initializing array in C

I tried to run this code:

#define ROW_CNT 8;
#define COLUMN_CNT 24;
#define FIRST_COLUMN 2;

unsigned int volume[ROW_CNT][COLUMN_CNT][ROW_CNT];

but I get the following errors:

expected identifier or '(' before ']' token

Why is that?

Upvotes: 0

Views: 77

Answers (3)

AusCBloke
AusCBloke

Reputation: 18492

A pre-processor definition, such as ROW_CNT, replaces any instances of the identifier in your code with the value it's defined as being. Therefore once the pre-processor has expanded your macros, your code will look like:

unsigned int volume[8;][24;][2;];

As you can see, the semicolon is included after 8, 24 and 2, since that's how you defined ROW_CNT, COLUMN_COUNT and FIRST_COUNT to be, and that's obviously not valid C syntax.

Remove the semicolons from the end of your #defines and the code will compile.

Upvotes: 1

Jay
Jay

Reputation: 24895

#define ROW_CNT 8; 
#define COLUMN_CNT 24; 
#define FIRST_COLUMN 2; 

should be

#define ROW_CNT 8
#define COLUMN_CNT 24
#define FIRST_COLUMN 2

semicolons should not be used for #define

Upvotes: 3

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

Take off the semicolons on your #defines.

The #define directives are handled by the preprocessing stage of compilation, which is all about text substitution. So, whenever the preprocessor performs text substitution, your program becomes

unsigned int volume[8;][24;][2;];

which isn't valid C.

Upvotes: 4

Related Questions