エラーがでる原因と解決策を教えてください。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ryusei
記事: 6
登録日時: 5年前

エラーがでる原因と解決策を教えてください。

#1

投稿記事 by ryusei » 5年前

どなたか教えていただけるとありがたいです。

先日から構造体の勉強をしています。
このようなコードをテキストを参考に作成しました。

code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//会員情報の構造体
struct LIST{
unsigned int number;
char name[256];
struct LIST *next;
} ;

int displayList(struct LIST *list){
while (list != NULL){
printf("%d:%s\n", list->number, list->name);
list = list->next;
}
return 0;
}

struct LIST* deleteAllList(struct LIST *list)
{
struct LIST *tmp;

while(list != NULL){
tmp = list->next;
free(list);
list = tmp;
}

return NULL;
}


struct LIST* addList(struct LIST *list, char *name, unsigned int number){
struct LIST *item;
struct LIST *top;

top = list;

item = (struct LIST*)malloc(sizeof(struct LIST));
item->number = number;
strcpy(item->name, name);
item->next = NULL;

//リストが空きの場合先頭に追加して終了
if(list == NULL){
list = item;
return list;
}

while (list != NULL){
list = list->next;
}

list->next = list;

return top;
}

struct LIST* deleteList(struct LIST *list, unsigned int number){
struct LIST *tmp;
struct LIST *top;

top = list;

//先頭ノードだけ別処理
if(list != NULL && list->number == number){
tmp = list->next;
free(list);

return tmp;
}
puts("---");
while(list != NULL){
if(list->next->number == number){
tmp = list->next->next;
free(list->next);
list->next = tmp;
return top;
}
list = list->next;
}

printf("number %d not found!\n", number);

return NULL;
}


int main(void)
{
int operation;
unsigned int inputnumber = 1;
unsigned int number = 1;
char inputname[256];
char buf[100];
int c;
struct LIST *list = NULL;

while (1){
puts("operation(1:add 2;delete etc:exit) = ");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%1d", &operation);
switch (operation){
case 1:
printf("add name = ");

fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%s", inputname);
buf[strlen(buf) - 1] = '\0';
list = addList(list, inputname, number);
displayList(list);
number++;
break;

case 2:
if(list == NULL){
puts("list is empty\n");
} else {
puts("delete number = ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
sscanf(buf, "%d", &inputnumber);
list = deleteList(list, inputnumber);
}
break;
default:
displayList(list);

list = deleteAllList(list);

displayList(list);

return 0;
}

}
}
/code
ここでおそらく、main関数内のcase(1)文
のfgetsあたりで二回目の入力行おうとすると、
Segmentation fault (コアダンプ)というエラーが出ます。
なぜなのか?ということと解決策を教えていただければ幸いです。

よろしくお願いいたします。

かずま

Re: エラーがでる原因と解決策を教えてください。

#2

投稿記事 by かずま » 5年前

ryusei さんが書きました:
5年前
ここでおそらく、main関数内のcase(1)文
のfgetsあたりで二回目の入力行おうとすると、
Segmentation fault (コアダンプ)というエラーが出ます。
なぜなのか?ということと解決策を教えていただければ幸いです。
addList で、
1回目は list が NULL なので、item の値を返して終わりです。
2回目は whileループで list が NULL になるまでリストを
たどりますが、そのあと NULL の list を使って list->next を
参照しています。

解決策

コード:

struct LIST* addList(struct LIST *list, char *name, unsigned int number)
{
	struct LIST *item = (struct LIST*)malloc(sizeof(struct LIST));
	item->number = number;
	strcpy(item->name, name);
	item->next = NULL;

	if (list == NULL) return item;

	struct LIST *p = list;
	while (p->next != NULL) p = p->next;
	p->next = item;
	return list;
}
送信ボタンを押す前に、プレビューボタンを押して
自分の投稿がどのように表示されるかを確認してください。
codeタグには [ ] が必要です。

かずま

Re: エラーがでる原因と解決策を教えてください。

#3

投稿記事 by かずま » 5年前

deleteList も間違っていますが、自分で直したんでしょうか?

コード:

struct LIST *deleteList(struct LIST *list, unsigned int number)
{
	struct LIST *p = list;
	if (p == NULL) return NULL;
	if (p->number == number) { //先頭ノードだけ別処理
		struct LIST *tmp = list->next;
		free(p);
		return tmp;
	}
	while (p->next != NULL && p->next->number != number) p = p->next;

	if (p->next == NULL) printf("number %d not found!\n", number);
	else {
		struct LIST *tmp = p->next->next;
		free(p->next);
		p->next = tmp;
	}
	return list;
}

返信

“C言語何でも質問掲示板” へ戻る