Jason
Jason

Reputation: 11363

C constants throwing compile-time errors

Several people have commented on my C code here, saying that I should use constants as loop counters, rather than hard-writing them. I agree with them, since that is my practice when writing Java code, but I'm having compile-time errors thrown when I try to use constants in array declarations and loop conditionals.

To declare a constant in C, the syntax is #define NAME value.

In my code, I have two constants,BUFFER is the file read buffer, and PACKED is the output array size.

I use BUFFER to initialize char inputBuffer[BUFFER]; as a global variable, which works, but when I try to use PACKED

#define PACKED    7; // this line is in the header of file, just below preprocessors
int packedCount;
char inputPack[PACKED]; //compression storage
for (packedCount=0; packedCount<= PACKED; packedCount++){ 

I get am error: expected ‘]’ before ‘;’ token at char inputPack[PACKED] AND
error: expected expression before ‘;’ token in the loop initialization line. Both errors disappear when I replace PACKED with 7.

Upvotes: 1

Views: 173

Answers (3)

Jonathan Wood
Jonathan Wood

Reputation: 67283

You obviously are not posting the code exactly as it appears in your source file.

At the very least, you are missing the semicolon after char inputPack[PACKED].

I strongly suspect that your real source has a semicolon at the end of your macro declaration, which would cause the error. Macro definitions should not be terminated with a semicolon.

Upvotes: 3

Erik
Erik

Reputation: 91320

Try using something other than PACKED, e.g. PACKEDSIZE. It could be that your compiler uses PACKED for something else (e.g. related to struct packing). Also, as other answers mention, you're lacking a ;

Upvotes: 1

Kai
Kai

Reputation: 511

there is a ; missing after char inputPack[PACKED]

Upvotes: 2

Related Questions