1

I am doing the following in C

#define MAX_DATA_SIZE 500;

struct reliable_state {

  char dataBuffer[MAX_DATA_SIZE]; 

}

i.e I want to use the #define constant as array size in structure declaration. But above code gives weird error

.c:36: error: expected ‘]’ before ‘;’ token

So is there any other way to do this?

1
  • 2
    Remove the semicolon on your #define line. It should be #define MAX_DATA_SIZE 500 Commented Oct 12, 2011 at 7:15

3 Answers 3

9

Yes you can, just remove ';' in your define line:

#define MAX_DATA_SIZE 500

With define you have compiler will actually 'see' your struct definition as

char dataBuffer[500;];

which is clearly erroneous.

Sign up to request clarification or add additional context in comments.

Comments

3

When you use #define, the macro on the right side is defined "as is". E.g. here, you've just have to correct it to

#define MAX_DATA_SIZE 500  /* no semicolon */

Comments

1

The syntax for a non-empty object-like macro definitions is

#define MACRO_IDENTIFIER    REPLACEMENT

Note that there is no terminating semicolon in this syntax, unlike for C declarations and statements. Your semicolon became part of the REPLACEMENT and was inserted where you used the macro identifier, yielding

char dataBuffer[500;];

which is a syntax error the compiler diagnosed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.