c - C 中的全局变量链表队列? (初始化元素不是常量)

标签 c linked-list global-variables

我正在从事一个涉及一系列队列的项目。这些队列在范围内是全局的,因为它们由一系列功能处理和修改。截至目前,我的实现引发了“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/

相关文章:

c - 定义linux中C程序允许的最大char[]数组大小

javascript - 在 GAS 中使用全局变量是否可取?有什么缺点吗?

javascript - REACT- 将需要经常更新的数据存储为全局变量而不是使用 setState() - 不推荐?

c - 使用链表的堆栈实现有什么问题?

c++链表删除函数留洞

data-structures - 如何在没有头指针的情况下找到单链表的前一个节点

jquery - 如何在 JQuery $.each 函数中编辑全局变量?

c - printf 中至少保留一位小数的 double 格式

c - 读取我的输入后,程序在 Code::blocks 中没有响应

c - 以下功能的目的是什么?