结构体指针引用,赋值问题
本帖最后由 焦糖橙子 于 2021-8-9 10:48 编辑问题在183行,case 4:delPerson(*contacts);break;
报错信息 no match for 'operator*' (operand type is 'Person')
该怎么改啊写成case 4:delPerson(**contacts);break;这样吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Person
{
char name;
char phone;
struct Person *next;
};
void getInput(struct Person* person);//获取输入联系人信息
void printPerson(struct Person* person);//打印联系人
void addPerson(struct Person** contacts);//增加新的联系人
void changePerson(struct Person* contacts);//更改已有联系人
void delPerson(struct Person** contacts);//删除已有联系人
struct Person* findPerson(struct Person* person);//查询已有联系人
void displayContacts(struct Person* person);//打印通讯录
void relaseContacts(struct Person** contacts);//结束程序,释放内存
void getInput(struct Person*person)
{
printf("请输入姓名:");
scanf("%s",person->name);
printf("请输入电话:");
scanf("%s",person->phone);
}
void addPerson(struct Person**contacts)
{
struct Person *person;
struct Person *temp;
person=(struct Person*)malloc(sizeof(struct Person));
if(person==NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(person);
if(*contacts!=NULL)
{
temp=*contacts;
*contacts=person;
person->next=temp;
}
else
{
*contacts=person;
person->next=NULL;
}
}
void printPerson(struct Person*person)
{
printf("联系人:%s\n",person->name);
printf("电话:%s\n",person->phone);
}
struct Person *findPerson(struct Person *contacts)
{
struct Person*current;
char input;
printf("请输入联系人:");
scanf("%s",input);
current =contacts;
while(current!=NULL&&strcmp(current->name,input))
{
current=current->next;
}
return current;
}
void changePerson(struct Person *contacts)
{
struct Person*person;
person=findPerson(contacts);
if(person==NULL)
{
printf("找不到该联系人!\n");
}
else
{
printf("请输入新的联系电话:");
scanf("%s",person->phone);
}
}
void delPerson(struct Person**contacts)
{
struct Person*tenp;
struct Person*person;
struct Person*current;
struct Person*previous;
person=findPerson(*contacts);
if(person==NULL)
{
printf("找不到该联系人!\n");
}
else
{
current=*contacts;
previous=NULL;
while(current!=NULL&¤t!=person)
{
previous=current;
current=current->next;
}
if(previous==NULL)
{
contacts=*current->next;
}
else
{
previous->next=current->next;
}
free(person);
}
}
void displayContacts(struct Person*contacts)
{
struct Person*current;
while(current!=NULL)
{
printPerson(current);
current=current->next;
}
}
void releaseContacts(struct Person **contacts)
{
struct Person*temp;
while(*contacts!=NULL)
{
temp=*contacts;
*contacts=(*contacts)->next;
free(temp);
}
}
int main(void)
{
int code;
struct Person *contacts=NULL;
struct Person *person;
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 1:插入新的联系人 ---|\n");
printf("|--- 2:查找已有联系人 ---|\n");
printf("|--- 3:更改已有联系人 ---|\n");
printf("|--- 4:删除已有联系人 ---|\n");
printf("|--- 5:显示当前通讯录 ---|\n");
printf("|--- 6:退出通讯录程序 ---|\n");
printf("|- Powered by FishC.com -|\n");
while(1)
{
printf("\n请输入指令代码:");
scanf("%d",&code);
switch(code)
{
case 1:addPerson(&contacts);break;
case 2:person = findPerson(contacts);
if(person==NULL)
{
printf("找不到该联系人!\n");
}
else
{
printPerson(contacts);
}
break;
case 3:changePerson(contacts);break;
case 4:delPerson(*contacts);break;
case 5:displayContacts(contacts);break;
case 6:goto END;
}
}
END:
relaseContacts(&contacts);
return 0;
}
显然应该改为
case 4:delPerson(& contacts);break; jackz007 发表于 2021-8-9 10:50
显然应该改为
{:10_285:}突然想通了 jackz007 发表于 2021-8-9 10:50
显然应该改为
改完还是有错,这什么意思啊....
ld returned 1 exit status 本帖最后由 jackz007 于 2021-8-9 11:33 编辑
焦糖橙子 发表于 2021-8-9 10:59
改完还是有错,这什么意思啊....
ld returned 1 exit status
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Person
{
char name;
char phone;
struct Person *next;
};
void getInput(struct Person*person)
{
printf("请输入姓名:");
scanf("%s",person->name);
printf("请输入电话:");
scanf("%s",person->phone);
}
void addPerson(struct Person**contacts)
{
struct Person *person;
struct Person *temp;
person=(struct Person*)malloc(sizeof(struct Person));
if(person==NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(person);
if(*contacts!=NULL)
{
temp=*contacts;
*contacts=person;
person->next=temp;
}
else
{
*contacts=person;
person->next=NULL;
}
}
void printPerson(struct Person*person)
{
printf("联系人:%s\n",person->name);
printf("电话:%s\n",person->phone);
}
struct Person * findPerson(struct Person * contacts)
{
struct Person*current;
char input;
printf("请输入联系人:");
scanf("%s",input);
current =contacts;
while(current!=NULL&&strcmp(current->name,input))
{
current=current->next;
}
return current;
}
void changePerson(struct Person *contacts)
{
struct Person*person;
person=findPerson(contacts);
if(person==NULL)
{
printf("找不到该联系人!\n");
}
else
{
printf("请输入新的联系电话:");
scanf("%s",person->phone);
}
}
void delPerson(struct Person ** contacts)
{
struct Person * tenp ;
struct Person * person ;
struct Person * current;
struct Person * previous ;
person = findPerson(* contacts) ;
if(person == NULL)
{
printf("找不到该联系人!\n") ;
}
else
{
current = * contacts ;
previous = NULL ;
while(current != NULL && current != person)
{
previous = current ;
current = current -> next ;
}
if(previous == NULL)
{
contacts = & current -> next ;
}
else
{
previous -> next = current -> next ;
}
free(person) ;
}
}
void displayContacts(struct Person*contacts)
{
struct Person*current;
while(current!=NULL)
{
printPerson(current);
current=current->next;
}
}
void releaseContacts(struct Person **contacts)
{
struct Person * temp ;
while(*contacts != NULL)
{
temp = * contacts ;
* contacts = temp -> next ;
free(temp) ;
}
}
int main(void)
{
int code;
struct Person *contacts=NULL;
struct Person *person;
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 1:插入新的联系人 ---|\n");
printf("|--- 2:查找已有联系人 ---|\n");
printf("|--- 3:更改已有联系人 ---|\n");
printf("|--- 4:删除已有联系人 ---|\n");
printf("|--- 5:显示当前通讯录 ---|\n");
printf("|--- 6:退出通讯录程序 ---|\n");
printf("|- Powered by FishC.com -|\n");
while(1)
{
printf("\n请输入指令代码:");
scanf("%d",&code);
switch(code)
{
case 1:addPerson(&contacts);break;
case 2:person = findPerson(contacts);
if(person==NULL)
{
printf("找不到该联系人!\n");
}
else
{
printPerson(contacts);
}
break;
case 3:changePerson(contacts);break;
case 4:delPerson(& contacts);break;
case 5:displayContacts(contacts);break;
case 6:goto END;
}
}
END:
releaseContacts(&contacts);
return 0;
}
只是保证可以正常编译,至于功能,请自行测试修改。 重新检查了一下,更正了功能2和功能5的错误
正确代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Person
{
char name;
char phone;
struct Person* next;
};
void getInput(struct Person* person);//获取输入联系人信息
void printPerson(struct Person* person);//打印联系人
void addPerson(struct Person** contacts);//增加新的联系人
void changePerson(struct Person* contacts);//更改已有联系人
void delPerson(struct Person** contacts);//删除已有联系人
struct Person* findPerson(struct Person* person);//查询已有联系人
void displayContacts(struct Person* person);//打印通讯录
void relaseContacts(struct Person** contacts);//结束程序,释放内存
void getInput(struct Person* person)
{
printf("请输入姓名:");
scanf("%s", person->name);
printf("请输入电话:");
scanf("%s", person->phone);
}
void addPerson(struct Person** contacts)
{
struct Person* person;
struct Person* temp;
person = (struct Person*)malloc(sizeof(struct Person));
if (person == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(person);
if (*contacts != NULL)
{
temp = *contacts;
*contacts = person;
person->next = temp;
}
else
{
*contacts = person;
person->next = NULL;
}
}
void printPerson(struct Person* person)
{
printf("联系人:%s\n", person->name);
printf("电话:%s\n", person->phone);
}
struct Person* findPerson(struct Person* contacts)
{
struct Person* current;
char input;
printf("请输入联系人:");
scanf("%s", input);
current = contacts;
while (current != NULL && strcmp(current->name, input))
{
current = current->next;
}
return current;
}
void changePerson(struct Person* contacts)
{
struct Person* person;
person = findPerson(contacts);
if (person == NULL)
{
printf("找不到该联系人!\n");
}
else
{
printf("请输入新的联系电话:");
scanf("%s", person->phone);
}
}
void delPerson(struct Person** contacts)
{
struct Person* tenp;
struct Person* person;
struct Person* current;
struct Person* previous;
person = findPerson(*contacts);
if (person == NULL)
{
printf("找不到该联系人!\n");
}
else
{
current = *contacts;
previous = NULL;
while (current != NULL && current != person)
{
previous = current;
current = current->next;
}
if (previous == NULL)
{
*contacts = current->next;
}
else
{
previous->next = current->next;
}
free(person);
}
}
void displayContacts(struct Person* contacts)
{
struct Person* current;
current = contacts;
while (current != NULL)
{
printPerson(current);
current = current->next;
}
}
void relaseContacts(struct Person** contacts)
{
struct Person* temp;
while (*contacts != NULL)
{
temp = *contacts;
*contacts = temp->next;
free(temp);
}
}
int main(void)
{
int code;
struct Person* contacts = NULL;
struct Person* person;
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 1:插入新的联系人 ---|\n");
printf("|--- 2:查找已有联系人 ---|\n");
printf("|--- 3:更改已有联系人 ---|\n");
printf("|--- 4:删除已有联系人 ---|\n");
printf("|--- 5:显示当前通讯录 ---|\n");
printf("|--- 6:退出通讯录程序 ---|\n");
printf("|- Powered by FishC.com -|\n");
while (1)
{
printf("\n请输入指令代码:");
scanf("%d", &code);
switch (code)
{
case 1:addPerson(&contacts); break;
case 2:person = findPerson(contacts);
if (person == NULL)
{
printf("找不到该联系人!\n");
}
else
{
printPerson(person);
}
break;
case 3:changePerson(contacts); break;
case 4:delPerson(&contacts); break;
case 5:displayContacts(contacts); break;
case 6:goto END;
}
}
END:
relaseContacts(&contacts);
return 0;
}
页:
[1]