単純なリンクリストを作成しましたが、を介してリストを反復処理するadd()
とdisplay()
、プログラムのセグメンテーション違反が発生します。
#include <stdlib.h>
#include <stdio.h>
typedef struct entry {
void *value;
struct entry *next;
} entry;
typedef struct list {
entry *items;
} list;
list *create(void) {
list *l;
l = malloc (sizeof(list));
l->items = malloc(sizeof(entry*));
l->items->next = NULL;
return l;
}
void add(list *l, void *value) {
entry *temp, *last, *new;
for (temp = l->items; temp != NULL; temp = temp->next) {
last = temp;
}
new = malloc(sizeof(*new));
new->value = value;
new->next = NULL;
last->next = new;
}
void display(list *l) {
entry *temp;
for (temp = l->items; temp != NULL; temp = temp->next) {
printf("%s\n", temp->value);
}
}
int main(void) {
list *l = create();
add(l, "item1");
add(l, "item2");
add(l, "item3");
add(l, "item4");
display(l);
return 0;
}
私はいくつかのマシンでコードをテストしましたが、それはいくつかのマシンで動作し、他のマシンでは動作しません。エラーの原因についてはわかりません。