试图使用链表输出字符串但是总是报错

这是在csdn的第一次提问です!

寒暄与问题de分割线
10.19
问题:试图使用链表输出字符串但是总是报错

想要达到的结果是main运行代码:

struct linklist* t1 = create();
insertnode(t1, “w”, “awa”, 188);
showlist(t1);

预期输出:

awa w 188

应该可以正常printf出两个双引号的内容

运行环境是visual studio的最新版本

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct linklist
{
    int num;
    char sex;
    char name;
    struct linklist* next;
};

struct linklist* create()
{
    struct linklist* headnode = (struct linklist*)malloc(sizeof(struct linklist));
    headnode->next = NULL;
    printf("创建成功\n");
    return headnode;
}

void insertnode(struct linklist* list,char sex, char name, int num)
{
    struct linklist* newnode = (struct linklist*)malloc(sizeof(struct linklist));
    newnode->name = name;
    newnode->sex = sex;
    newnode->num = num;
    newnode->next = list->next;
    list->next = newnode;
    return;
    
}

void showlist(struct linklist* list)
{
    struct linklist* pmove = (struct linklist*)malloc(sizeof(struct linklist));
    pmove = list->next;
    while (pmove != NULL)
    {
        printf("%c %c %d\n", pmove->name, pmove->sex, pmove->num);
        pmove = pmove->next;
    }
    printf("输出完成");
    return;
}

int main(void)
{
    struct linklist* t1 = create();
    insertnode(t1, 'w', 'awa', 188);
    insertnode(t1, 'qwqw', '=', 130);
    showlist(t1);

    return 0;
}



上述代码的运行结果是:
PS:因为链表可以延长所以这是有两个insertnode的结果


创建成功
= w 130
a w 188
输出完成


这上面是一段可以跑的代码 PS:我所能写出的最接近预期结果的代码了qwq
但是还是很明显与我的预期相差太多
(我好像看到了vs报告了常量截断所以最后输出的字符串都只是我预期的最后一个)

尝试过这样一种解决办法:
鉴于曾经写过下面的代码

#include<stdio.h>
#include<string.h>
int main(void)
{
    typedef struct awq1
    {
        int num;
        int tel;
        char address[30];
    }awa;

    struct awq1 qwq;
    qwq.num = 10;
    qwq.tel = 1234;
    strcpy_s(qwq.address, "emmmm,guess");
    printf("%d %d %s", qwq.num, qwq.tel, qwq.address);

    awa qqq;
    qqq.num = 100;
    qqq.tel = 12345;
    strcpy_s(qqq.address, "emmmm,guessssss");
    printf("%d %d %s", qqq.num, qqq.tel, qqq.address);

    return 0;

}

上面这一串代码可以成功printf出字符串
所以我尝试了下面的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct linklist
{
    int num;
    char sex[20];
    char name[30];
};

void givedata(struct linklist* list, int num, char sex[20], char name[30])
{
    strcpy_s(list->name, name);
    strcpy_s(list->sex, sex);
}

int main(void)
{
    struct linklist tryA;
    givedata(&tryA, 10, "man", "测试1");
    printf("%s是%s", tryA.name, tryA.sex);
    
    //啊啊啊可恶啊还是报错!!
    //报错内容: error C2664: “void givedata(linklist *,int,char [],char [])”: 无法将参数 3 从“const char [4]”转换为“char []”

}

还尝试过几种方法也都是同样在报错C2664同时还报错其它包括不限于
error C3863: 不可指定数组类型“char []
(这个报错是在第一个代码块的struct linklist{}中将char name改成char name[30]之后的报错
其实vs还同时报了其他错,总之错的离谱、惨不忍睹qwq)

希望有人能指点或者示范一下,多谢了多谢了。

第一次编辑10.19
最新情况:
这是之前尝试的解决方案稍微改了一点点

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct linklist
{
    int num;
    char sex[20];
    char name[30];
};

void givedata(struct linklist* list, int num, const char sex[10], const char name[20])
{
    strcpy_s(list->name, name);
    strcpy_s(list->sex, sex);
}

int main(void)
{
    struct linklist tryA;
    givedata(&tryA, 10, "man", "测试1");
    printf("%s是%s", tryA.name, tryA.sex);
    
    //我看到报错就缺了个const限定符就给补上了
    //然后这代码他就跑起来了
    //还成功了

}

这串代码终于成功跑出了我想要的结果
不过在我把这个超简化的链表模型还原回链表的时候出现了下面的情况:
这是修改后的链表代码:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lin
{
    int tel;
    char sex[20];
    char name[30];
    struct lin* next;
};


struct lin* create()
{
    struct lin* headnode = (struct lin*)malloc(sizeof(struct lin));
    headnode->next = NULL;
    return headnode;
}

void insertnode(struct lin* list, const char name[30], const char sex[20], int tel)//开始了最关键这一步qwq
{
    struct lin* newnode = (struct lin*)malloc(sizeof(struct lin));
    newnode->name = name;
    newnode->sex = sex;
    newnode->tel = tel;
    newnode->next = list->next;
    list->next = newnode;
    return;
}

void showlist(struct lin* list)
{
    struct lin* pmove = (struct lin*)malloc(sizeof(struct lin));
    pmove = list->next;
    if (pmove != NULL)
    {
        printf("%s是个%s,tel是%d\n", pmove->name, pmove->sex, pmove->tel);
        pmove = pmove->next;
    }
    printf("输出完毕");
    return;
}


int main(void)
{
    struct lin* list = create();
    insertnode(list, "测试1号", "man", 123456);
    insertnode(list, "try2hao", "woman", 234775);
    showlist(list);

    return 0;

}

结果还是报错了

void insertnode(struct lin* list, const char name[30], const char sex[20], int tel)//开始了最关键这一步qwq
{
    struct lin* newnode = (struct lin*)malloc(sizeof(struct lin));
    newnode->name = name;  //error C3863: 不可指定数组类型“char [30]”
    newnode->sex = sex;  //error C3863: 不可指定数组类型“char [30]”
    newnode->tel = tel;
    newnode->next = list->next;
    list->next = newnode;
    return;
}

刚以为自己解决了问题结果当头一棒敲下来
请问有人知道这个报错怎么解决吗,还希望解答一下多谢多谢

你可以看下我单链表的博客,对比一下,希望可以帮到你。

问题我终于是自己解决掉了
/*
机缘巧合我无意发现自己新代码没有用到strcpy_s
无意义#include<string.h>
拍头自罚

*/
我发现自己忽略了新的解决方案中自己使用的是strcpy_s进行的对链表内的name和sex进行的赋值
再对链表进行修改之后终于是完全输出了自己理想的结果
下面是最后解决的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lin
{
    int tel;
    char sex[20];
    char name[30];
    struct lin* next;
};


struct lin* create()
{
    struct lin* headnode = (struct lin*)malloc(sizeof(struct lin));
    headnode->next = NULL;
    return headnode;
}

void insertnode(struct lin* list, const char name[30], const char sex[20], int tel)
{
    struct lin* newnode = (struct lin*)malloc(sizeof(struct lin));
    strcpy_s(newnode->name, name);
    strcpy_s(newnode->sex, sex);
    newnode->tel = tel;
    newnode->next = list->next;
    list->next = newnode;
    return;
}

void showlist(struct lin* list)
{
    struct lin* pmove = (struct lin*)malloc(sizeof(struct lin));
    pmove = list->next;
    if (pmove != NULL)
    {
        printf("%s是个%s,tel是%d\n", pmove->name, pmove->sex, pmove->tel);
        pmove = pmove->next;
    }
    printf("输出完毕");
    return;
}


int main(void)
{
    struct lin* list = create();
    insertnode(list, "测试1号", "man", 123456);
    insertnode(list, "try2hao", "woman", 234775);
    showlist(list);


    return 0;

}

最后只要补上删除节点的函数这个链表就完满了
完满万岁!!
希望我的问题和解答对大家有帮助,欸嘿~