关于C语言malloc内存申请失败的问题
本帖最后由 牛奶刺身 于 2019-2-4 16:35 编辑总体代码如下(实现通讯录):(问题描述在下面{:10_266:} )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Cont
{
char name;
char phone;
char company;
struct Cont *next;
};
void addPerson(struct Cont **);
struct Cont *findPerson(struct Cont **);
void changePerson(struct Cont **);
void delPerson(struct Cont **);
void displayPerson(struct Cont **);
void Input(struct Cont *);
void Print(struct Cont *);
void Input(struct Cont *news)
{
printf("请输入姓名:");
scanf("%s", news->name);
printf("请输入电话:");
scanf("%s", news->phone);
printf("请输入公司:");
scanf("%s", news->company);
getchar();
}
void Print(struct Cont *display)
{
printf("姓名:%s\n", display->name);
printf("电话:%s\n", display->phone);
printf("公司:%s\n", display->company);
}
void addPerson(struct Cont **data)
{
struct Cont *person, *temp;
static struct Cont *tail;
person = (struct Cont *)malloc(sizeof(struct Cont));
if (person == NULL);
{
printf("内存分配失败!\n");
exit(1);
}
Input(person);
while (1)
{
if (*data == NULL)
{
*data = person;
person->next = NULL;
}
else
{
tail->next = person;
person->next = NULL;
break;
}
}
}
struct Cont *findPerson(struct Cont **data)
{
char input;
struct Cont *search;
search = *data;
scanf("%s", input);
getchar();
while (search != NULL)
{
if (!strcmp(search->name, input))
{
break;
}
else
{
search = search->next;
}
}
if (search == NULL)
{
printf("抱歉,未找到该联系人!\n");
return search;
}
Print(search);
return search;
}
void changePerson(struct Cont **data)
{
struct Cont *search;
char ch;
printf("请输入要更改的联系人:");
search = findPerson(data);
if (search == NULL)
{
printf("请问是否添加新的联系人(Y/N)?");
do
{
scanf("%c", &ch);
getchar();
} while (ch != 'Y' && ch != 'N');
}
else
{
printf("请问是否更改该联系人信息(Y/N)?");
do
{
scanf("%c", &ch);
getchar();
} while (ch != 'Y' && ch != 'N');
}
if (ch == 'N')
{
return;
}
addPerson(&search);
}
void delPerson(struct Cont **data)
{
struct Cont *previous, *temp;
char ch;
printf("请输入要删除的联系人:");
previous = findPerson(data);
if (previous == NULL)
{
printf("未找到该联系人!\n");
return;
}
printf("请问是否删除该联系人信息(Y/N)?");
do
{
scanf("%c", &ch);
getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'N')
{
return;
}
temp = previous;
previous = previous->next;
free(temp);
}
void displayPerson(struct Cont **data)
{
while (*data != NULL)
{
Print(*data);
*data = (*data)->next;
}
}
int main(void)
{
struct Cont *list = NULL;
int index;
printf("=======================\n");
printf("= 欢迎使用本程序!=\n");
printf("= (指令功能如下) : =\n");
printf("=1.插入新的联系人; =\n");
printf("=2.查找已有联系人; =\n");
printf("=3.更改已有联系人; =\n");
printf("=4.删除已有联系人; =\n");
printf("=5.显示当前通讯录; =\n");
printf("=6.退出通讯录程序 =\n");
printf("=======================\n");
do
{
printf("请输入指令:");
scanf("%d", &index);
getchar();
switch (index)
{
case 1 : addPerson(&list); break;
case 2 : printf("请输入要查找的联系人:");findPerson(&list); break;
case 3 : changePerson(&list); break;
case 4 : delPerson(&list); break;
case 5 : displayPerson(&list); break;
case 6 : break;
default : break;
}
} while (index != 6);
printf("感谢使用本程序!\n");
free(list);
return 0;
}
出问题的地方是addPerson函数,运行时直接返回申请内存失败
求大神解答一下,拜托拜托!
我的天,犯了个很二的错误,在判断语句的后面加了个分号{:10_277:}哭唧唧,没脸见人 本帖最后由 jackz007 于 2019-2-5 04:29 编辑
你的代码问题很多,几乎无法实现预设的所有功能,我已经帮你全面修改了一下,当然,可能还有未发现的错误。
由于改动的地方较多,就不一一叙述,楼主就参照自己的代码参考吧。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Cont
{
char name;
char phone;
char company;
struct Cont *next;
};
void addPerson(struct Cont **);
struct Cont *findPerson(struct Cont **);
void changePerson(struct Cont **);
void delPerson(struct Cont **);
void displayPerson(struct Cont **);
void Input(struct Cont *);
void Print(struct Cont *);
void Input(struct Cont * news)
{
printf("请输入姓名:") ;
scanf("%s", news->name) ;
printf("请输入电话:") ;
scanf("%s", news->phone) ;
printf("请输入公司:") ;
scanf("%s", news->company) ;
}
void Print(struct Cont *display)
{
printf("姓名:%s\n", display->name);
printf("电话:%s\n", display->phone);
printf("公司:%s\n", display->company);
}
void addPerson(struct Cont **data)
{
struct Cont * person , * tail , * temp ;
person = (struct Cont *)malloc(sizeof(struct Cont)) ;
if (person == NULL) {
printf("内存分配失败!\n") ;
exit(1) ;
}
Input(person) ;
person -> next = NULL ;
if (* data == NULL) {
* data = person ;
} else {
tail = * data ;
while (tail -> next != NULL) tail = tail -> next ;
tail -> next = person ;
}
}
struct Cont * findPerson(struct Cont **data)
{
char input ;
struct Cont * search ;
search = * data ;
printf("请输入姓名:") ;
scanf("%s" , input) ;
while (search != NULL) {
if (! strcmp(search -> name , input)) break ;
search = search -> next ;
}
return search ;
}
struct Cont * SearchPerson(struct Cont **data)
{
struct Cont * search ;
search = findPerson(data) ;
if(search == NULL) printf("抱歉,联系人未找到\n") ;
else Print(search) ;
return search ;
}
void changePerson(struct Cont **data)
{
struct Cont * search ;
char ch ;
search = findPerson(data) ;
if (search == NULL) {
printf("抱歉,没有找到该联系人信息\n") ;
printf("请问是否添加新的联系人(Y/N)?") ;
do {
ch = getchar() ;
} while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
if (ch == 'Y' || ch == 'y') {
addPerson(data) ;
printf("成功添加联系人信息\n") ;
}
} else {
printf("请问是否更改该联系人信息(Y/N)?") ;
do {
ch = getchar() ;
} while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
if (ch == 'Y' || ch == 'y') {
Input(search) ;
printf("成功修改联系人信息\n") ;
}
}
}
void delPerson(struct Cont **data)
{
struct Cont * previous , * temp ;
char ch ;
bool f ;
temp = findPerson(data) ;
if (temp == NULL) {
printf("未找到该联系人!\n");
} else {
printf("请问是否删除该联系人信息(Y/N)?") ;
do {
ch = getchar() ;
} while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
if(ch == 'Y' || ch == 'y') {
f = false ;
if (temp == * data) {
* data = temp -> next ;
f = true ;
} else {
previous = * data ;
while (previous != NULL) {
if (previous -> next == temp) {
previous -> next = temp -> next ;
f = true ;
break ;
}
}
}
if (f) {
free(temp) ;
printf("成功删除该联系人!\n") ;
} else {
printf("抱歉,删除联系人失败!\n") ;
}
}
}
}
void displayPerson(struct Cont **data)
{
struct Cont * walk ;
walk = * data ;
if (walk != NULL) {
while (walk != NULL) {
Print(walk) ;
walk = walk -> next;
}
} else {
printf("抱歉,尚无数据录入") ;
}
}
int main(void)
{
struct Cont *list = NULL , * p ;
char c ;
printf("=======================\n");
printf("= 欢迎使用本程序!=\n");
printf("= (指令功能如下) : =\n");
printf("=1.插入新的联系人; =\n");
printf("=2.查找已有联系人; =\n");
printf("=3.更改已有联系人; =\n");
printf("=4.删除已有联系人; =\n");
printf("=5.显示当前通讯录; =\n");
printf("=6.退出通讯录程序 =\n");
printf("=======================\n");
do {
printf("请输入指令:") ;
gets(c) ;
switch (c) {
case '1' : addPerson(&list); break ;
case '2' : SearchPerson(&list) ; break ;
case '3' : changePerson(&list); break;
case '4' : delPerson(&list); break ;
case '5' : displayPerson(&list); break ;
case '6' : break ;
default: break ;
}
} while (c != '6') ;
printf("感谢使用本程序!\n") ;
free(list) ;
return 0 ;
}
jackz007 发表于 2019-2-5 04:13
你的代码问题很多,几乎无法实现预设的所有功能,我已经帮你全面修改了一下,当然,可能还有未发现的错 ...
昂,多谢啦,在一点点改
页:
[1]