C言語 リスト構造体の勉強しているのですが、
一日半悩みに悩んだ末、答えが出なかったので質問を聞いてください。
情報を構造体へ任意の回数分登録した後に単方向リストで繋げた構造体を
参照するプログラムを組んでいるのですが、構造体領域の確保をmallocで行った部分で
エラーが発生し進まなくなりました。
・主なエラー場所は自作関数内での領域確保の部分でエラーが発生し
構造体の情報が扱えていない事だと自分は考えていてデバッグ作業にとりかかっていたのですが
もう1日半も前に進まなくなったのでヒントだけでも頂ければと・・・
宜しくお願い致します。
以下ソースになります。
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct _LIST{ int key; char name[100 +1]; struct _LIST *next; }LIST; struct LIST *input_str(); struct LIST *show_str(); int main(void) { //まずはリスト構造体へ登録 input_str(); //次に参照 show_str(); return 0; } struct LIST *input_str() { LIST *head; struct LIST *temp; //先頭ポインタの初期化 int tkey; char tname[100+1]; int i;** //入力繰り返し判定用 //head->dummy用構造体の作成 if ((head = (LIST *) malloc(sizeof(LIST))) == NULL) { //error printf("malloc error\n"); exit(EXIT_FAILURE); } head->key = -99; //error strcpy(head->name,"dummy"); //error do{ LIST *tmp = head; //1回目のループにはdummy構造体のheadポインタが入っている printf("番号:"); scanf("%d",&tkey); printf("名前:"); scanf("%s",tname); //一つの構造体の領域を確保! if ((head = (LIST *) malloc(sizeof(LIST))) == NULL) { //error printf("malloc error\n"); exit(EXIT_FAILURE); } //入力されたデータを構造体へ head->key = tkey; //error strcpy(head->name,tname); //error head->next = tmp; //error printf("繰り返し入力しますか? Yes(1)/NO(0):\n"); scanf("%d",&i);}} }while(i == 1); return (head); } struct LIST *show_str() { //未完成 }
回答3件
あなたの回答
tips
プレビュー