Не могу понять, почему этот segfault в c, valgrind сказал, что ошибка находится в строке 25. Это программа для управления медицинской студией, когда e == 1 пациент прибывает, поэтому он должен быть добавлен в очередь, e == 2 пациент посещается, поэтому первый элемент в очереди должен быть удален, когда e == 0 студия закрывается, а программа должна печатать список пациентов, оставшихся в алфавитном порядке, и $.Почему этот segfault в c программе
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN (101)
typedef struct _item {
char *name;
struct _item *next;
} item;
void insert(item* tail, item* next){
item* new = (item*)malloc(sizeof(item));
new->name = (char*)malloc(MAXLEN*sizeof(char));
new->next = NULL;
scanf("%s", new->name);
if (tail == NULL){
tail = new;
next = tail;
}
else
next->next = new;
}
void examination(item *tail){
item *patient;
if (tail->next == NULL)
tail=NULL;
else{
patient = tail;
tail = tail->next;
free(patient);
}
}
int cmp(const void *a, const void *b){
return strcmp(*((char**)a) , *((char**)b));
}
int main(){
int e=1, counter=0, i=0;
item *tail = (item*)malloc(sizeof(item));
item *next;
char **remained;
tail = NULL;
next = tail;
while (e != 0){
scanf("%d", &e);
switch (e){
case 1:
insert(tail, next);
break;
case 2:
examination(tail);
case 0:
break;
default:
return 1;
}
}
next = tail;
while (next != NULL){
counter ++;
next = next->next;
}
next = tail;
remained = (char**)malloc(counter*sizeof(char*));
while(i < counter){
remained[i] = next->name;
next = next->next;
i++;
}
qsort(remained, counter, sizeof(item), cmp);
next = tail;
while (next != NULL){
printf("%s\n", next->name);
next = next->next;
}
printf("$\n");
return 0;
}
функция вставки кажется неправильной [требуется указатель на указатель] (http://stackoverflow.com/a/18307020/1673391) –
И строка 25 - это какая строка, пожалуйста? – alk
[Пожалуйста, не выдавайте результат 'malloc()'] (http://stackoverflow.com/a/605858/3233393). – Quentin