PDA

View Full Version : #define id (expression) Directive generates error



urvil
26th May 2015, 22:00
Hi,

i come to some unpredictable state,

first declared in file1.h
/* File1.h */

typedef struct {
int i;
} struct_1;
#define size_1 10
#define DATA_LEN (size_1 + sizeof(struct_1) )

importing file1.h in to file2.h

#include "File1.h"

char text_data[ DATA_LEN ]; // here i am getting error expected ']' before ';' token

stampede
29th May 2015, 10:03
Two problems with your code:
1) use include guards (https://en.wikipedia.org/wiki/Include_guard) in headers
2) do not define variables in header files, this way each *.c file which includes your header will get its own copy of the variable

to solve 2), use the "extern" specifier (https://en.wikipedia.org/wiki/External_variable#Example_.28C_programming_languag e.29)

apart from that, your example seems to compile just fine on gcc 4.5:


// this code is wrong, correct it by applying 1) and 2)
//
//
// file1.h
typedef struct {
int i;
} struct_1;
#define size_1 10
#define DATA_LEN (size_1 + sizeof(struct_1) )

//file2.h
#include "file1.h"

char text_data[ DATA_LEN ];

//main.c
#include "file2.h"

int main(){
return 0;
}

// compiled ok with :
// gcc main.c -Werror -Wall -pedantic

wysota
29th May 2015, 11:41
Hi,

i come to some unpredictable state,

first declared in file1.h
/* File1.h */

typedef struct {
int i;
} struct_1;
#define size_1 10
#define DATA_LEN (size_1 + sizeof(struct_1) )

importing file1.h in to file2.h

#include "File1.h"

char text_data[ DATA_LEN ]; // here i am getting error expected ']' before ';' token

Is that the exact code you have? Are you sure there is no semicolon in any of the defines?

urvil
31st May 2015, 10:44
Thanks wysota,
Your thinking is right, there were a semicolon in define line but there is lots of spaces in between semicolon n sentance
#define DATA_LEN (size_1 + sizeof(struct_1) ) (long string of spaces) ........................................... ;
I am wondering how this happen...

Thanks for the reply