从队列中弹出时C中的分段错误

时间:2016-11-02 20:50:35

标签: c data-structures queue

我遇到了问题,我试图调试它,但我找不到任何东西

#include <stdio.h>
#include <stdlib.h>

struct Process {
    int id;
    int at;
    int bt;
    int rt;
    struct Process *next;
} *tmp;

struct Queue {
    struct Process *head, *tail;
};

struct Process *pop(struct Queue *queue) {
    if (queue->head == NULL) {
        return NULL;
    }
    tmp = queue->head;
    queue->head = queue->head->next;
    return tmp;
}

int main() {
    struct Queue queues[3];
    struct Process *working;
    for (int i = 0; i < 3; i++) {
        queues[i].head = NULL;
    }
    FILE *processesFile = fopen("processes.txt", "r");
    while (!feof(processesFile)) {
        fscanf(processesFile, "%d %d %d", &id, &at, &bt);
        push(&queues[0], id, at, bt);
    }
    working = pop(&queues[0]); // HERE IS THE PROBLEM, It is removing the first element but causing segmentation error
    return 0;
}

我删除了推送部分,我可以成功地推送5个值并打印整个队列而没有任何问题

1 个答案:

答案 0 :(得分:0)

我没有看到push(&amp; queues [0],id,at,bt);的定义。 fscanf(processesFile,“%d%d%d”,&amp; id,&amp; at,&amp; bt);这里使用变量id,at和bt但它们实际上并不作为结构变量访问。