When I tried to initialize a global array that contains structure elements, I got the " error: initializer element is not constant"
a.h
#define MAX_MSG_NUM 20
#define NAME_SIZE 15
#define MAX_QUE_NUM 10
typedef struct {
int index;
int tid;
int front;
int rear;
char name[NAME_SIZE];
char msgbuf[MAX_MSG_NUM];
} THREAD;
typedef enum {
I1 = 0,
I2 = 1,
I3 = 2,
I4 = 3
} DMTHREAD;
a.c
THREAD a[MAX_MSG_NUM];
THREAD b[MAX_MSG_NUM];
THREAD c[MAX_MSG_NUM];
THREAD T[MAX_QUE_NUM] = {
{I1, 0, 0, 0, "CONFIG1", a[MAX_MSG_NUM]},
{I2, 0, 0, 0, "CONFIG2", b[MAX_MSG_NUM]},
{I3, 0, 0, 0, "CONFIG3", c[MAX_MSG_NUM]},
0
};
GCC compiler.
bash-3.2$ gcc -g a.h a.c
a.c:8: error: initializer element is not constant
a.c:8: error: (near initialization for 'T[0].msgbuf[0]')
a.c:9: error: initializer element is not constant
a.c:9: error: (near initialization for 'T[1].msgbuf[0]')
a.c:10: error: initializer element is not constant
a.c:10: error: (near initialization for 'T[2].msgbuf[0]')
Thanks for help.
Enlightened by your answers, I changed the code and it works fine now.
MSG T[MAX_QUE_NUM] = {
{I1, 0, 0, 0, "CONFIG1", {0}},
{I2, 0, 0, 0, "CONFIG2", {0}},
{I3, 0, 0, 0, "CONFIG3", {0}},
0,
}
Thank you for your answers.