凌域 2023-03-20 15:15 采纳率: 70%
浏览 29
已结题

C语言链表创建方面问题

下图为一个创建链表程序,我想询问下为何我对一个结点赋值后程序就自动结束了?如下图

img

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

struct _node{
    int num;
    char name[10];
    char a[10];
    int age;
    struct _node *next;
};

struct _node* create()
{
    struct _node* head=(struct _node*)malloc(sizeof(struct _node));
    head->num=0;
    head->name[10]="0";
    head->a[10]="0";
    head->age=0;
    head->next=NULL;
    struct _node* pcurrent=head;
    while(1)
    {
        int number,ag;
        char na[10],b[10];
        printf("请输入学号\n");
        scanf("%d",&number);
        printf("请输入姓名\n");
        scanf("%s",na);    
        printf("请输入性别\n");
        scanf("%s",b);    
        printf("请输入年纪");
        scanf("%d",&ag);
        if(number==0||na[10]=="0"||b[10]=="0"||ag==0)
        {
            return head;
        }
        struct _node *newnode=(struct _node*)malloc(sizeof(struct _node));
        newnode->num=number;
        strcpy(newnode->name[10],na[10]);
        strcpy(newnode->a[10],b[10]);
        newnode->age=ag;
        pcurrent->next=newnode;
        pcurrent=pcurrent->next;
    }
    pcurrent=head->next;
    printf("输出已经赋值的链表");
    while(pcurrent!=NULL)
    {
        printf("%d",pcurrent->num);
        pcurrent=pcurrent->next;
    }
    return head;    
    
};
    
    
int main()
{
        struct _node *head=create();    
        
}
  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2023-03-20 15:19
    关注

    看返回值是崩溃了,na[10]=="0"||b[10]=="0" 这都是越界访问。字符串判断相等用strcmp(na,"0") == 0这样子
    strcpy也用的不对,改为
    strcpy(newnode->name,na);
    na[10]是访问na数组的第10个元素,首先类型是char,不是数组或指针类型;其次第10个元素越界了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 创建了问题 3月20日

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存