关于#c语言#的问题:字符常量作为参数传递


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student
{
    char name[20];
    char id[20];
    struct Student* next;
}student;

//创建链表
student* Create(){
    student* head = (student*)malloc(sizeof(student));//头指针
    student *end,*new;//尾节点,新节点
    end = new = (student*)malloc(sizeof(student));
    printf("请输入姓名,学号:");
    setbuf(stdin,NULL);
    scanf("%s %s",new->name,new->id);
    while(1){
        end = new;
        end->next = NULL;
        head->next = end;
        new = (student*)malloc(sizeof(student));
        printf("是否继续(Y/N)");
        setbuf(stdin,NULL);
        int choice = getchar();
        if (choice == 'N' || choice == 'n')
        {
            break;
        }
        printf("请输入下一个同学的姓名,学号:");
        setbuf(stdin,NULL);
        scanf("%s %s",new->name,new->id);
    }    
    free(new);
    return head;
}
//头插
student* headNode(student* head,char name[],char id[]){
    student* newNode = (student*)malloc(sizeof(student));
     strcpy(newNode->name,&name);
    strcpy(newNode->id,&id);
    newNode->next = head->next;
    head->next = newNode;
    return head;
}
//删除
// student* DeleteNode(student* head,   ){
    
// }
//遍历链表
void printLink(student* head){
    student* p = head->next;
    while (p != NULL)
    {
        printf("姓名:%s,学号:%s",p->name,p->id);
        printf("\n");
        p = p->next;
    }
}
int main()
{
    student* link = Create();
    printLink(link);
    headNode(link,'ba','002');
    printf("\n插入之后为:");
    printLink(link);
    return 0;
}



img

 请输入姓名,学号:                     aa  001
 是否继续(Y/N)n
 姓名:aa,学号:001
 请插入:姓名:ab,学号:200
 姓名:       aa,学号:          001
 PS D:lvsc\dmlprojects Whelloworld>

现在哪里有问题呢?

插入 ba 002 后输出的为什么会是ab 200

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^