分配到一个结构数组失败,"错误:初始元素不是常数QUOT; [英] Assignment to a struct array failed with "error: initializer element is not constant"
本文介绍了分配到一个结构数组失败,"错误:初始元素不是常数QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试图初始化包含结构元素的全局数组,我得到了错误:初始元素不是常量
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;
交流转换器
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编译器。
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]')
感谢您的帮助。
通过你的回答启发,我改变了code和现在工作得很好。
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,
}
感谢您的答案。
推荐答案
A [MAX_MSG_NUM]
确实是不恒定的。它还表示发
实例,这恰好是先前定义的数组之外,并且不匹配你应该通过有类型(的char [ ]
)。
a[MAX_MSG_NUM]
is indeed non-constant. It also denotes a Thread
instance, which happens to be outside the array you defined earlier, and does not match the type you should pass there (char[]
).
因此,它的是错误这么多层次。
So it is wrong on so many levels.
这篇关于分配到一个结构数组失败,"错误:初始元素不是常数QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文