鱼C论坛

 找回密码
 立即注册
查看: 845|回复: 5

[已解决]结构体指针引用,赋值问题

[复制链接]
发表于 2021-8-9 10:38:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 焦糖橙子 于 2021-8-9 10:48 编辑

问题在183行,case 4:delPerson(*contacts);break;
报错信息[Error] no match for 'operator*' (operand type is 'Person')
该怎么改啊写成case 4:delPerson(**contacts);break;这样吗?

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Person
  5. {
  6.         char name[40];
  7.         char phone[20];
  8.         struct Person *next;
  9. };

  10. void getInput(struct Person* person);//获取输入联系人信息
  11. void printPerson(struct Person* person);//打印联系人
  12. void addPerson(struct Person** contacts);//增加新的联系人
  13. void changePerson(struct Person* contacts);//更改已有联系人
  14. void delPerson(struct Person** contacts);//删除已有联系人
  15. struct Person* findPerson(struct Person* person);//查询已有联系人
  16. void displayContacts(struct Person* person);//打印通讯录
  17. void relaseContacts(struct Person** contacts);//结束程序,释放内存

  18. void getInput(struct Person*person)
  19. {
  20.         printf("请输入姓名:");
  21.         scanf("%s",person->name);
  22.         printf("请输入电话:");
  23.         scanf("%s",person->phone);
  24. }

  25. void addPerson(struct Person**contacts)
  26. {
  27.         struct Person *person;
  28.         struct Person *temp;
  29.        
  30.         person=(struct Person*)malloc(sizeof(struct Person));
  31.         if(person==NULL)
  32.         {
  33.                 printf("内存分配失败!\n");
  34.                 exit(1);
  35.         }
  36.        
  37.         getInput(person);
  38.        
  39.         if(*contacts!=NULL)
  40.         {
  41.                 temp=*contacts;
  42.                 *contacts=person;
  43.                 person->next=temp;
  44.         }
  45.         else
  46.         {
  47.                 *contacts=person;
  48.                 person->next=NULL;
  49.         }
  50. }

  51. void printPerson(struct Person*person)
  52. {
  53.         printf("联系人:%s\n",person->name);
  54.         printf("电话:%s\n",person->phone);
  55. }

  56. struct Person *findPerson(struct Person *contacts)
  57. {
  58.         struct Person*current;
  59.         char input[40];
  60.        
  61.         printf("请输入联系人:");
  62.         scanf("%s",input);
  63.        
  64.         current =contacts;
  65.         while(current!=NULL&&strcmp(current->name,input))
  66.         {
  67.                 current=current->next;
  68.         }
  69.         return current;
  70. }

  71. void changePerson(struct Person *contacts)
  72. {
  73.         struct Person*person;
  74.        
  75.         person=findPerson(contacts);
  76.         if(person==NULL)
  77.         {
  78.                 printf("找不到该联系人!\n");
  79.         }
  80.         else
  81.         {
  82.                 printf("请输入新的联系电话:");
  83.                 scanf("%s",person->phone);
  84.         }
  85. }

  86. void delPerson(struct Person**contacts)
  87. {
  88.         struct Person*tenp;
  89.         struct Person*person;
  90.         struct Person*current;
  91.         struct Person*previous;
  92.        
  93.         person=findPerson(*contacts);
  94.         if(person==NULL)
  95.         {
  96.                 printf("找不到该联系人!\n");
  97.         }
  98.         else
  99.         {
  100.                 current=*contacts;
  101.                 previous=NULL;
  102.        
  103.                 while(current!=NULL&&current!=person)
  104.                 {
  105.                         previous=current;
  106.                         current=current->next;
  107.                  }
  108.                  if(previous==NULL)
  109.                  {
  110.                          contacts=*current->next;
  111.                  }
  112.                  else
  113.                  {
  114.                          previous->next=current->next;
  115.                  }

  116.                  free(person);
  117.         }
  118. }

  119. void displayContacts(struct Person*contacts)
  120. {
  121.         struct Person*current;
  122.         while(current!=NULL)
  123.         {
  124.                 printPerson(current);
  125.                 current=current->next;
  126.         }
  127. }

  128. void releaseContacts(struct Person **contacts)
  129. {
  130.         struct Person*temp;
  131.         while(*contacts!=NULL)
  132.         {
  133.                 temp=*contacts;
  134.                 *contacts=(*contacts)->next;
  135.                 free(temp);
  136.         }
  137. }

  138. int main(void)
  139. {
  140.         int code;
  141.         struct Person *contacts=NULL;
  142.         struct Person *person;
  143.        
  144.         printf("| 欢迎使用通讯录管理程序 |\n");
  145.         printf("|--- 1:插入新的联系人 ---|\n");
  146.         printf("|--- 2:查找已有联系人 ---|\n");
  147.         printf("|--- 3:更改已有联系人 ---|\n");
  148.         printf("|--- 4:删除已有联系人 ---|\n");
  149.         printf("|--- 5:显示当前通讯录 ---|\n");
  150.         printf("|--- 6:退出通讯录程序 ---|\n");
  151.         printf("|- Powered by FishC.com -|\n");
  152.        
  153.         while(1)
  154.         {
  155.                 printf("\n请输入指令代码:");
  156.                 scanf("%d",&code);
  157.                 switch(code)
  158.                 {
  159.                         case 1:addPerson(&contacts);break;
  160.                         case 2:person = findPerson(contacts);
  161.                         if(person==NULL)
  162.                         {
  163.                                 printf("找不到该联系人!\n");
  164.                         }
  165.                         else
  166.                         {
  167.                                 printPerson(contacts);
  168.                         }
  169.                         break;
  170.                         case 3:changePerson(contacts);break;
  171.                         case 4:delPerson(*contacts);break;
  172.                         case 5:displayContacts(contacts);break;
  173.                         case 6:goto END;
  174.                 }
  175.         }
  176. END:
  177.         relaseContacts(&contacts);
  178.        
  179.        
  180.         return 0;
  181. }
复制代码
最佳答案
2021-8-9 10:50:00
        显然应该改为
  1. case 4:delPerson(& contacts);break;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 10:50:00 | 显示全部楼层    本楼为最佳答案   
        显然应该改为
  1. case 4:delPerson(& contacts);break;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-9 10:55:31 | 显示全部楼层

突然想通了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-9 10:59:55 | 显示全部楼层

改完还是有错,这什么意思啊....

[Error] ld returned 1 exit status
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-9 11:30:56 | 显示全部楼层
本帖最后由 jackz007 于 2021-8-9 11:33 编辑
焦糖橙子 发表于 2021-8-9 10:59
改完还是有错,这什么意思啊....

[Error] ld returned 1 exit status

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Person
  5. {
  6.         char name[40];
  7.         char phone[20];
  8.         struct Person *next;
  9. };

  10. void getInput(struct Person*person)
  11. {
  12.         printf("请输入姓名:");
  13.         scanf("%s",person->name);
  14.         printf("请输入电话:");
  15.         scanf("%s",person->phone);
  16. }

  17. void addPerson(struct Person**contacts)
  18. {
  19.         struct Person *person;
  20.         struct Person *temp;
  21.         
  22.         person=(struct Person*)malloc(sizeof(struct Person));
  23.         if(person==NULL)
  24.         {
  25.                 printf("内存分配失败!\n");
  26.                 exit(1);
  27.         }
  28.         
  29.         getInput(person);
  30.         
  31.         if(*contacts!=NULL)
  32.         {
  33.                 temp=*contacts;
  34.                 *contacts=person;
  35.                 person->next=temp;
  36.         }
  37.         else
  38.         {
  39.                 *contacts=person;
  40.                 person->next=NULL;
  41.         }
  42. }

  43. void printPerson(struct Person*person)
  44. {
  45.         printf("联系人:%s\n",person->name);
  46.         printf("电话:%s\n",person->phone);
  47. }

  48. struct Person * findPerson(struct Person * contacts)
  49. {
  50.         struct Person*current;
  51.         char input[40];
  52.         
  53.         printf("请输入联系人:");
  54.         scanf("%s",input);
  55.         
  56.         current =contacts;
  57.         while(current!=NULL&&strcmp(current->name,input))
  58.         {
  59.                 current=current->next;
  60.         }
  61.         return current;
  62. }

  63. void changePerson(struct Person *contacts)
  64. {
  65.         struct Person*person;
  66.         
  67.         person=findPerson(contacts);
  68.         if(person==NULL)
  69.         {
  70.                 printf("找不到该联系人!\n");
  71.         }
  72.         else
  73.         {
  74.                 printf("请输入新的联系电话:");
  75.                 scanf("%s",person->phone);
  76.         }
  77. }

  78. void delPerson(struct Person ** contacts)
  79. {
  80.         struct Person * tenp     ;
  81.         struct Person * person   ;
  82.         struct Person * current  ;
  83.         struct Person * previous ;
  84.         
  85.         person = findPerson(* contacts) ;
  86.         if(person == NULL)
  87.         {
  88.                 printf("找不到该联系人!\n") ;
  89.         }
  90.         else
  91.         {
  92.                 current = * contacts ;
  93.                 previous = NULL      ;
  94.         
  95.                 while(current != NULL && current != person)
  96.                 {
  97.                         previous = current        ;
  98.                         current = current -> next ;
  99.                  }
  100.                  if(previous == NULL)
  101.                  {
  102.                          contacts = & current -> next ;
  103.                  }
  104.                  else
  105.                  {
  106.                          previous -> next = current -> next ;
  107.                  }

  108.                  free(person) ;
  109.         }
  110. }

  111. void displayContacts(struct Person*contacts)
  112. {
  113.         struct Person*current;
  114.         while(current!=NULL)
  115.         {
  116.                 printPerson(current);
  117.                 current=current->next;
  118.         }
  119. }

  120. void releaseContacts(struct Person **contacts)
  121. {
  122.         struct Person * temp ;
  123.         while(*contacts != NULL)
  124.         {
  125.                 temp = * contacts         ;
  126.                 * contacts = temp -> next ;
  127.                 free(temp)                ;
  128.         }
  129. }

  130. int main(void)
  131. {
  132.         int code;
  133.         struct Person *contacts=NULL;
  134.         struct Person *person;
  135.         
  136.         printf("| 欢迎使用通讯录管理程序 |\n");
  137.         printf("|--- 1:插入新的联系人 ---|\n");
  138.         printf("|--- 2:查找已有联系人 ---|\n");
  139.         printf("|--- 3:更改已有联系人 ---|\n");
  140.         printf("|--- 4:删除已有联系人 ---|\n");
  141.         printf("|--- 5:显示当前通讯录 ---|\n");
  142.         printf("|--- 6:退出通讯录程序 ---|\n");
  143.         printf("|- Powered by FishC.com -|\n");
  144.         
  145.         while(1)
  146.         {
  147.                 printf("\n请输入指令代码:");
  148.                 scanf("%d",&code);
  149.                 switch(code)
  150.                 {
  151.                         case 1:addPerson(&contacts);break;
  152.                         case 2:person = findPerson(contacts);
  153.                         if(person==NULL)
  154.                         {
  155.                                 printf("找不到该联系人!\n");
  156.                         }
  157.                         else
  158.                         {
  159.                                 printPerson(contacts);
  160.                         }
  161.                         break;
  162.                         case 3:changePerson(contacts);break;
  163.                         case 4:delPerson(& contacts);break;
  164.                         case 5:displayContacts(contacts);break;
  165.                         case 6:goto END;
  166.                 }
  167.         }
  168. END:
  169.         releaseContacts(&contacts);
  170.         return 0;
  171. }
复制代码

        只是保证可以正常编译,至于功能,请自行测试修改。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-9 16:43:11 | 显示全部楼层
重新检查了一下,更正了功能2和功能5的错误

正确代码
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Person
  5. {
  6.     char name[40];
  7.     char phone[20];
  8.     struct Person* next;
  9. };

  10. void getInput(struct Person* person);//获取输入联系人信息
  11. void printPerson(struct Person* person);//打印联系人
  12. void addPerson(struct Person** contacts);//增加新的联系人
  13. void changePerson(struct Person* contacts);//更改已有联系人
  14. void delPerson(struct Person** contacts);//删除已有联系人
  15. struct Person* findPerson(struct Person* person);//查询已有联系人
  16. void displayContacts(struct Person* person);//打印通讯录
  17. void relaseContacts(struct Person** contacts);//结束程序,释放内存

  18. void getInput(struct Person* person)
  19. {
  20.     printf("请输入姓名:");
  21.     scanf("%s", person->name);
  22.     printf("请输入电话:");
  23.     scanf("%s", person->phone);
  24. }

  25. void addPerson(struct Person** contacts)
  26. {
  27.     struct Person* person;
  28.     struct Person* temp;

  29.     person = (struct Person*)malloc(sizeof(struct Person));
  30.     if (person == NULL)
  31.     {
  32.         printf("内存分配失败!\n");
  33.         exit(1);
  34.     }

  35.     getInput(person);

  36.     if (*contacts != NULL)
  37.     {
  38.         temp = *contacts;
  39.         *contacts = person;
  40.         person->next = temp;
  41.     }
  42.     else
  43.     {
  44.         *contacts = person;
  45.         person->next = NULL;
  46.     }
  47. }

  48. void printPerson(struct Person* person)
  49. {
  50.     printf("联系人:%s\n", person->name);
  51.     printf("电话:%s\n", person->phone);
  52. }

  53. struct Person* findPerson(struct Person* contacts)
  54. {
  55.     struct Person* current;
  56.     char input[40];

  57.     printf("请输入联系人:");
  58.     scanf("%s", input);

  59.     current = contacts;
  60.     while (current != NULL && strcmp(current->name, input))
  61.     {
  62.         current = current->next;
  63.     }
  64.     return current;
  65. }

  66. void changePerson(struct Person* contacts)
  67. {
  68.     struct Person* person;

  69.     person = findPerson(contacts);
  70.     if (person == NULL)
  71.     {
  72.         printf("找不到该联系人!\n");
  73.     }
  74.     else
  75.     {
  76.         printf("请输入新的联系电话:");
  77.         scanf("%s", person->phone);
  78.     }
  79. }

  80. void delPerson(struct Person** contacts)
  81. {
  82.     struct Person* tenp;
  83.     struct Person* person;
  84.     struct Person* current;
  85.     struct Person* previous;

  86.     person = findPerson(*contacts);
  87.     if (person == NULL)
  88.     {
  89.         printf("找不到该联系人!\n");
  90.     }
  91.     else
  92.     {
  93.         current = *contacts;
  94.         previous = NULL;

  95.         while (current != NULL && current != person)
  96.         {
  97.             previous = current;
  98.             current = current->next;
  99.         }
  100.         if (previous == NULL)
  101.         {
  102.             *contacts = current->next;
  103.         }
  104.         else
  105.         {
  106.             previous->next = current->next;
  107.         }

  108.         free(person);
  109.     }
  110. }

  111. void displayContacts(struct Person* contacts)
  112. {
  113.     struct Person* current;
  114.     current = contacts;
  115.     while (current != NULL)
  116.     {
  117.         printPerson(current);
  118.         current = current->next;
  119.     }
  120. }

  121. void relaseContacts(struct Person** contacts)
  122. {
  123.     struct Person* temp;
  124.     while (*contacts != NULL)
  125.     {
  126.         temp = *contacts;
  127.         *contacts = temp->next;
  128.         free(temp);
  129.     }
  130. }

  131. int main(void)
  132. {
  133.     int code;
  134.     struct Person* contacts = NULL;
  135.     struct Person* person;

  136.     printf("| 欢迎使用通讯录管理程序 |\n");
  137.     printf("|--- 1:插入新的联系人 ---|\n");
  138.     printf("|--- 2:查找已有联系人 ---|\n");
  139.     printf("|--- 3:更改已有联系人 ---|\n");
  140.     printf("|--- 4:删除已有联系人 ---|\n");
  141.     printf("|--- 5:显示当前通讯录 ---|\n");
  142.     printf("|--- 6:退出通讯录程序 ---|\n");
  143.     printf("|- Powered by FishC.com -|\n");

  144.     while (1)
  145.     {
  146.         printf("\n请输入指令代码:");
  147.         scanf("%d", &code);
  148.         switch (code)
  149.         {
  150.         case 1:addPerson(&contacts); break;
  151.         case 2:person = findPerson(contacts);
  152.             if (person == NULL)
  153.             {
  154.                 printf("找不到该联系人!\n");
  155.             }
  156.             else
  157.             {
  158.                 printPerson(person);
  159.             }
  160.             break;
  161.         case 3:changePerson(contacts); break;
  162.         case 4:delPerson(&contacts); break;
  163.         case 5:displayContacts(contacts); break;
  164.         case 6:goto END;
  165.         }
  166.     }
  167. END:
  168.     relaseContacts(&contacts);

  169.     return 0;
  170. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 22:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表