鱼C论坛

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

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

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

struct Phone{
        char name[32];
        long int number;
        struct Phone *next;
};

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

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

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

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

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

        now = *head;
        before = NULL;

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

        getInput(&new);

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

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

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

        phone = phonebook;
        
        while (phone != NULL){
                if (!strcmp(phone->name, name)){
                        break;
                }
                phone = phone->next;
        }

        return phone;
}

void change(struct Phone **head){//更改
        struct Phone *before;//前
        struct Phone *now;//现
        struct Phone *new;//后
        char name[32];
        long int number;

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

        now = *head;

        while (now != NULL){
                if (!strcmp(now->name, name)){
                        break;
                }
                now = now->next;
        }

        if(now == NULL){
                printf("抱歉,没有找到。\n");
        }
        else{
                free(now);
                now = *head;
                before = NULL;

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

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

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

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

void delete(struct Phone **head, char *name){//删除
        struct Phone *before;//前
        struct Phone *now;//现
 
        now = *head;
        before = NULL;

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

        if (now == NULL){//没有相等的或者head进来就是NULL
                printf("找不到匹配的节点\n");
                return ;
        }
        else{
                if (before == NULL){//第一个就是要删除的节点
                        *head = now->next;
                }
                else{
                        before->next = now->next;
                }

                free(now);
        }
}

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

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

        putchar('\n');
}

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

        while (head != NULL){
                temp = head;
                head = head->next;
                free(temp);
        }
}

int main(void){
        struct Phone *head;
        struct Phone *temp;
        char name[32];
        int i;

        welcome();

        while (1){
                printf("\n请输入指令代码:");
                scanf("%d", &i);
                switch (i){
                        case 1: insert(&head); break;
                        case 2: printf("请输入联系人:");
                                scanf("%s", name);
                                temp = searchEvent(head, name);
                                printf("电话号码:temp->number");
                                break;
                           case 3: change(&head); break;
                        case 4: printf("请输入联系人:");
                                scanf("%s", name);
                                delete(&head, name); break;
                        case 5: print(head); break;
                        case 6: release(head);exit(1);
                }
        }

        return 0;
}
最佳答案
2022-12-7 14:27:52
本帖最后由 jhq999 于 2022-12-7 22:20 编辑

静态数组指针不能被赋值
//new->name = name;
int i=0;
while((new->name[i]=name[i++]));
或者
int i=0;
while(i<32)new->name[i]=name[i++];

最佳答案

查看完整内容

静态数组指针不能被赋值 或者
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

静态数组指针不能被赋值
//new->name = name;
int i=0;
while((new->name[i]=name[i++]));
或者
int i=0;
while(i<32)new->name[i]=name[i++];
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-7 21:55:47 | 显示全部楼层
御坂已经自己完成了! 3W1R$PID~1_SLSY~)[4LZP7.png }UY8U8)SDJ~A{EV12GZPT5K.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

恭喜
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-20 20:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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