大学のコースにリンクリストデータ構造を実装しようとしていますが、コードを実行すると、次の行でEXC_BAD_ACCESS(code = 1、address = 0x8)エラーが発生します。
temp->next = (ptrtonode) malloc(sizeof(struct node));
以下はコード全体です。
#include <stdio.h>
#include <stdlib.h>
typedef struct node *ptrtonode;
typedef ptrtonode header;
struct node
{
int data;
ptrtonode next;
};
ptrtonode create(int n)
{
int i;
header temphead = NULL;
ptrtonode temp = temphead;
for(i=0;i<n;i++)
{
temp->next = (ptrtonode) malloc(sizeof(struct node));
printf("Enter data for node %d: ", i+1);
scanf("%d", &temp->next->data);
temp = temp->next;
}
temp->next = NULL;
return temphead;
}
int main(int argc, const char * argv[])
{
header head;
int n;
printf("How many nodes do you wish to create?");
scanf("%d", &n);
head = create(n);
}
どんな助けでもいただければ幸いです。皆さんありがとう!