程序收到信号 SIGSEGV,C 中的分段错误

问题描述

我已经运行了这个程序,这是关于 C 中的链表

  • 第一步:我在列表中输入数字元素为三
  • 第二步:我在每个元素中输入数据。 但是在这三个元素中,我无法为其输入数据,因此我决定进行调试,而不是遇到此错误

This is my error

    #include<stdio.h>
#include <stdlib.h>
struct ThueBao
{
    char HoTen[20];
    char sdt[10][11];
    char diachi[30];
    int soSDT;
};
struct DSLKD
{
    struct ThueBao data;
    struct DSLKD *lienket;
} ;
//typedef struct DSLKD *node;
void NhapThueBao(struct ThueBao *tb)
{
    fflush(stdin);
    printf("\nNhap ho ten thue bao: ");
    gets(tb->HoTen);
    fflush(stdin);
    printf("\nNhap dia chi: ");
    gets(tb->diachi);
    printf("Nhap so SDT cua TB: ");
    scanf("%d",&tb->soSDT);
    int i;
    for(i=0;i<tb->soSDT;i++)
    {
        fflush(stdin);
        printf("\nSo dien thoai thu %d: ",i+1);
        gets(tb->sdt[i]);   
    }
    
}
struct DSLKD *KhoiTaoDS(int n)
{
    struct DSLKD *dau = NULL; //DAI DIEN CHO NOTE DAU cua DS
    struct DSLKD *newnode = NULL;// DAI DIEN CHO NOTE MOI SE DC THEM VAO
    struct DSLKD *p = NULL;// THAY THE CHO NOTE DA CO TRONG DANH SACH
    int i;
    for(i=0;i<n;i++)
    {
        
        newnode = (struct DSLKD*)malloc(sizeof(struct DSLKD));// cap phat o nho cho node nay
        printf("Nhap phan tu thu %d",i+1);
        fflush(stdin);
        NhapThueBao(&newnode->data);
        newnode->lienket = NULL;
        if(dau == NULL)
        {
            dau = newnode;
        }
        else
        {
            p = dau;
            while(p != NULL)
            {
                p = p->lienket;// tro toi phan tu tiep theo
                p->lienket = newnode;// phan tu tiep theo dc tro la newnode
            }
        }
        }
    return dau;
};

void InThueBao (struct ThueBao tb)
{
    printf("\nHo ten thue bao: %s\nDia chi TB: %s\nThue bao su dung %d SDT",tb.HoTen,tb.diachi,tb.soSDT);
    int i;
    for(i=0;i<tb.soSDT;i++)
    {
        printf("\nSDT thu %d : %s",i+1,tb.sdt[i]);
    }
}

 void inDanhBa(struct DSLKD *d)
{
    struct DSLKD *p;
    p = d;
    while(p !=  NULL)
    {
        InThueBao(p->data);
        p = p->lienket;
    }
}
void themvaodau(struct DSLKD **dau,struct ThueBao tb)
{
    struct DSLKD *q=(struct DSLKD*)malloc(sizeof(struct DSLKD));
    q->data = tb;
    q->lienket = *dau;
    *dau = q;
}
void main()
{
    struct ThueBao tb;
    struct DSLKD *dau = NULL;
    dau =(struct DSLKD*)malloc(sizeof(struct DSLKD));
    int n;
    printf("Nhap so luong phan tu: ");
    scanf("%d",&n);
    dau = KhoiTaoDS(n);
    inDanhBa(dau);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)