我正在从事一个涉及一系列队列的项目。这些队列在范围内是全局的,因为它们由一系列功能处理和修改。截至目前,我的实现引发了“Initializer element is not constant”标志。我明白为什么,在大多数情况下,但我想知道是否有其他方法可以实现这一目标。 (即包含每个队列的数组?)这可能吗?
主.c
LIST* queue0 = ListCreate(); //0
LIST* queue1 = ListCreate(); //1
LIST* queue2 = ListCreate(); //2
int main(){......}
ListCreate 发生如下:
实现.c
LIST *ListCreate()
{
int popped = popList();
if (popped != -1){
//Create list
lists[popped].currSize = 0;
return &lists[popped];
}
else{
return NULL;
}
}
(请记住,我需要在不使用 malloc 的情况下构建链表。)
最佳答案
LIST* queue0 = NULL;
LIST* queue1 = NULL;
LIST* queue2 = NULL;
void initQueues(void) {
queue0 = ListCreate();
queue1 = ListCreate();
queue2 = ListCreate();
}
void main(int argc, char *argv[]) {
initQueues();
// ... (i.e. other stuff)
}
关于c - C 中的全局变量链表队列? (初始化元素不是常量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354234/