小编use*_*560的帖子

分段故障问题.试图在C中实现双链表FIFO队列

我在使用此代码时遇到问题.我是C的新手,据我所知,我正在使用malloc操作.

#include "fifo.h"
#include <stdlib.h>

/* add a new element to a fifo */
void Enqueue( fifo* queue, int customerId)
{
   //allocate memory for the element being added
   //initialize fifo_element
   fifo_element *temp;
   temp = (fifo_element*)malloc(sizeof(fifo_element));
   temp->customerId = customerId;
   temp->prev = NULL;
   temp->next = NULL;

   //if the queue is empty, add the element to the start
   if(&queue->head == NULL){
      queue->head = queue->tail = temp;
      return;
   }
   else{
      queue->tail->next = temp;
      temp->prev = queue->tail;
      queue->tail = temp;
      return;      
   }   
}
Run Code Online (Sandbox Code Playgroud)

如果没有出现分段错误,我无法执行此操作:

queue->tail->next …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers segmentation-fault

4
推荐指数
1
解决办法
364
查看次数

标签 统计

c ×1

malloc ×1

pointers ×1

segmentation-fault ×1