鱼C论坛

 找回密码
 立即注册
查看: 2252|回复: 3

[已解决]这个错误是什么情况呀?什么叫char *’赋值给‘char[32]’时类型不兼容?

[复制链接]
发表于 2022-12-7 14:27:51 | 显示全部楼层 |阅读模式
10鱼币
{~44T0JMTNQABXQ]8PDIFVU.png
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Phone{
  4.         char name[32];
  5.         long int number;
  6.         struct Phone *next;
  7. };

  8. void welcome(void);//打印欢迎和使用说明
  9. void getInput(struct Phone **phone);//录入数据
  10. void insert(struct Phone **head);//插入新的联系人
  11. struct Phone *searchEvent(struct Phone *phonebook, char *name);//查找已有的联系人
  12. void change(struct Phone **head);//更改
  13. void delete(struct Phone **head, char *name);//删除
  14. void print(struct Phone *head);//打印
  15. void release(struct Phone *head);//释放内存

  16. void welcome(void){//打印欢迎和使用说明
  17.         printf("| 欢迎使用通讯录管理程序 |\n");
  18.         printf("|--- 1:插入新的联系人 ---|\n");
  19.         printf("|--- 2:查找已有联系人 ---|\n");
  20.         printf("|--- 3:更改已有联系人 ---|\n");
  21.         printf("|--- 4:删除已有联系人 ---|\n");
  22.         printf("|--- 5:显示当前通讯录 ---|\n");
  23.         printf("|--- 6:退出通讯录程序 ---|\n");
  24.         printf("|--------------------|\n");
  25. }

  26. void getInput(struct Phone **phone){//录入数据
  27.         struct Phone *temp = *phone;

  28.         printf("请输入姓名:");
  29.         scanf("%s", temp->name);
  30.         printf("请输入电话号码:");
  31.         scanf("%ld", &temp->number);
  32. }

  33. void insert(struct Phone **head){//插入新的联系人
  34.         struct Phone *before;//前
  35.         struct Phone *now;//现
  36.         struct Phone *new;//后

  37.         now = *head;
  38.         before = NULL;

  39.         new = (struct Phone *)malloc(sizeof(struct Phone));//给新的链表分配内存
  40.         if(new == NULL){
  41.                 printf("内存分配失败了。\n");
  42.                 exit(1);
  43.         }

  44.         getInput(&new);

  45.         while (now != NULL && now->number < new->number){//现在的链表的number值要不小于i
  46.                 before = now;
  47.                 now = now->next;
  48.         }

  49.         if (before == NULL){//*head是空的
  50.                 *head = new;//把新生成的链表放在第一个
  51.         }
  52.         else{
  53.                 before->next = new;//前一个的next要指向现在的
  54.         }
  55. }

  56. struct Phone *searchEvent(struct Phone *phonebook, char *name){//查找已有的联系人
  57.         struct Phone *phone;

  58.         phone = phonebook;
  59.        
  60.         while (phone != NULL){
  61.                 if (!strcmp(phone->name, name)){
  62.                         break;
  63.                 }
  64.                 phone = phone->next;
  65.         }

  66.         return phone;
  67. }

  68. void change(struct Phone **head){//更改
  69.         struct Phone *before;//前
  70.         struct Phone *now;//现
  71.         struct Phone *new;//后
  72.         char name[32];
  73.         long int number;

  74.         printf("请输入要更改的联系人:");
  75.         scanf("%s", name);

  76.         now = *head;

  77.         while (now != NULL){
  78.                 if (!strcmp(now->name, name)){
  79.                         break;
  80.                 }
  81.                 now = now->next;
  82.         }

  83.         if(now == NULL){
  84.                 printf("抱歉,没有找到。\n");
  85.         }
  86.         else{
  87.                 free(now);
  88.                 now = *head;
  89.                 before = NULL;

  90.                 new = (struct Phone *)malloc(sizeof(struct Phone));//给新的链表分配内存
  91.                 if(new == NULL){
  92.                         printf("内存分配失败了。\n");
  93.                         exit(1);
  94.                 }

  95.                 new->name = name;
  96.                 printf("请输入电话号码:");
  97.                 scanf("%ld", &new->number);

  98.                 while (now != NULL && now->number < new->number){//现在的链表的number值要不小于输入的
  99.                         before = now;
  100.                         now = now->next;
  101.                 }

  102.                 if (before == NULL){//*head是空的
  103.                         *head = new;//把新生成的链表放在第一个
  104.                 }
  105.                 else{
  106.                         before->next = new;//前一个的next要指向现在的
  107.                 }
  108.         }
  109. }

  110. void delete(struct Phone **head, char *name){//删除
  111.         struct Phone *before;//前
  112.         struct Phone *now;//现

  113.         now = *head;
  114.         before = NULL;

  115.         while (now != NULL && strcmp(now->name, name)){//现在的链表的name要相等
  116.                 before = now;
  117.                 now = now->next;
  118.         }

  119.         if (now == NULL){//没有相等的或者head进来就是NULL
  120.                 printf("找不到匹配的节点\n");
  121.                 return ;
  122.         }
  123.         else{
  124.                 if (before == NULL){//第一个就是要删除的节点
  125.                         *head = now->next;
  126.                 }
  127.                 else{
  128.                         before->next = now->next;
  129.                 }

  130.                 free(now);
  131.         }
  132. }

  133. void print(struct Phone *head){//打印
  134.         struct Phone *now;

  135.         now = head;
  136.         while (now != NULL){
  137.                 printf("姓名:%s  ", now->name);
  138.                 printf("电话号码:%ld\n", &now->number);
  139.                 now = now->next;
  140.         }

  141.         putchar('\n');
  142. }

  143. void release(struct Phone *head){//释放内存
  144.         struct Phone *temp;

  145.         while (head != NULL){
  146.                 temp = head;
  147.                 head = head->next;
  148.                 free(temp);
  149.         }
  150. }

  151. int main(void){
  152.         struct Phone *head;
  153.         struct Phone *temp;
  154.         char name[32];
  155.         int i;

  156.         welcome();

  157.         while (1){
  158.                 printf("\n请输入指令代码:");
  159.                 scanf("%d", &i);
  160.                 switch (i){
  161.                         case 1: insert(&head); break;
  162.                         case 2: printf("请输入联系人:");
  163.                                 scanf("%s", name);
  164.                                 temp = searchEvent(head, name);
  165.                                 printf("电话号码:temp->number");
  166.                                 break;
  167.                            case 3: change(&head); break;
  168.                         case 4: printf("请输入联系人:");
  169.                                 scanf("%s", name);
  170.                                 delete(&head, name); break;
  171.                         case 5: print(head); break;
  172.                         case 6: release(head);exit(1);
  173.                 }
  174.         }

  175.         return 0;
  176. }
复制代码
最佳答案
2022-12-7 14:27:52
本帖最后由 jhq999 于 2022-12-7 22:20 编辑

静态数组指针不能被赋值
  1. //new->name = name;
  2. int i=0;
  3. while((new->name[i]=name[i++]));
复制代码

或者
  1. int i=0;
  2. while(i<32)new->name[i]=name[i++];
复制代码

最佳答案

查看完整内容

静态数组指针不能被赋值 或者
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-7 14:27:52 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-12-7 22:20 编辑

静态数组指针不能被赋值
  1. //new->name = name;
  2. int i=0;
  3. while((new->name[i]=name[i++]));
复制代码

或者
  1. int i=0;
  2. while(i<32)new->name[i]=name[i++];
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-7 21:55:47 | 显示全部楼层
御坂已经自己完成了! 3W1R$PID~1_SLSY~)[4LZP7.png }UY8U8)SDJ~A{EV12GZPT5K.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-7 21:58:26 From FishC Mobile | 显示全部楼层
本帖最后由 xiaotubie 于 2022-12-7 22:02 编辑

恭喜
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-17 08:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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