C 如何避免链表的分段错误

C 如何避免链表的分段错误,c,linked-list,segmentation-fault,C,Linked List,Segmentation Fault,我想做一个代码,使用链表,从终端获取输入,然后打印出一个表 在这个例子中,我从元素表上传了一些信息 我得到了分割错误 有人能帮我理解为什么 #include <stdio.h> #include <stdlib.h> typedef struct element{ char name[20]; char symbol[20]; float atom_weight; struct Element* next; } element; /*

我想做一个代码,使用链表,从终端获取输入,然后打印出一个表

在这个例子中,我从元素表上传了一些信息

我得到了分割错误

有人能帮我理解为什么

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

typedef struct element{
    char name[20];
    char symbol[20];
    float atom_weight;
    struct Element* next;
} element;


/* Add a new node to the top of a list */
element* insert_top(char name[20], char symbol[20], float atom_weight, element* head) {
    element *new_element;
    new_element = (element *) malloc(sizeof(element));
    new_element->name[20] = name;
    new_element->symbol[20] = symbol;
    new_element->atom_weight = atom_weight;

    new_element->next= head;
    head = new_element;
    printf("Top inserted");
    return head;
}

element* table=NULL;
int main()
{
    int choice=1, i=0;
    char name[256];
    char symbol[256];
    float atom_weight;

    printf("%d", choice);
    while (choice!=0){

        printf("\n Please enter element name:");
        scanf("%s", name);

        printf("\n Please enter element symbol:");
        scanf("%s", symbol);

        printf("\n Please enter atomic weight:");
        scanf("%f", &atom_weight);


        //printf("%s, %s,...Weight %f",name, symbol, atom_weight);
        insert_top(name, symbol, atom_weight, table);

        i=i+1;
        printf("\nDo you want to continue (Y=1/N=0)? ");
        scanf("%d", &choice); //You should add the space before %c, not after

    }
    printf("Out of cycle\n");
    printf("Size of table %lu\n", sizeof(table));

    printf("Weight %f",table->atom_weight);


    while (table->next != NULL){
        printf("\nElement: %s \t\t Symbol: %s  \t\t Atomic weight: %f\n",table[i].name, table[i].symbol,table[i].atom_>
        //printf("ciao");
        table=table->next;
    }
}

#包括
#包括
类型定义结构元素{
字符名[20];
字符符号[20];
浮动原子重量;
结构元素*next;
}元素;
/*将新节点添加到列表顶部*/
元素*插入顶部(字符名称[20],字符符号[20],浮动原子重量,元素*头部){
元素*新元素;
新的元素=(元素*)malloc(sizeof(元素));
新建元素->名称[20]=名称;
新建元素->符号[20]=符号;
新元素->原子重量=原子重量;
新建元素->下一步=头部;
head=新的_元素;
printf(“顶部插入”);
回流头;
}
元素*表=空;
int main()
{
int-choice=1,i=0;
字符名[256];
字符符号[256];
浮动原子重量;
printf(“%d”,选项);
while(选项!=0){
printf(“\n请输入元素名称:”);
scanf(“%s”,名称);
printf(“\n请输入元素符号:”);
扫描频率(“%s”,符号);
printf(“\n请输入原子量:”);
scanf(“%f”&原子重量);
//printf(“%s,%s,…重量%f”,名称、符号、原子重量);
插入顶部(名称、符号、原子重量、表格);
i=i+1;
printf(“\N是否继续(Y=1/N=0)?”;
scanf(“%d”,&choice);//您应该在%c之前添加空格,而不是之后添加空格
}
printf(“周期外\n”);
printf(“表的大小%lu\n”,sizeof(表));
printf(“重量%f”,表格->原子重量);
while(表->下一步!=NULL){
printf(“\n元素:%s\t\t符号:%s\t\t原子量:%f\n”,表[i]。名称,表[i]。符号,表[i]。原子>
//printf(“ciao”);
表=表->下一步;
}
}

您需要使用strcpy复制字符串,请参阅
插入\u top
未定义
元素
,应为
元素
。 小调不施放malloc结果

仍然存在一些问题,字符大小限制,需要检查scanf返回代码

最后需要释放malloc内存

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

typedef struct element{
    char name[20];
    char symbol[20];
    float atom_weight;
    struct element* next;
} element;


/* Add a new node to the top of a list */
element* insert_top(char name[20], char symbol[20], float atom_weight, element* head) {
    element *new_element = malloc(sizeof(element));
    strcpy(new_element->name, name);
    strcpy(new_element->symbol, symbol);
    new_element->atom_weight = atom_weight;

    new_element->next= head;
    printf("Top inserted");
    return new_element;
}

int main()
{
    element* table=NULL;
    int choice=1;

    printf("%d", choice);
    while (choice!=0){
        char name[256];
        char symbol[256];
        float atom_weight;

        printf("\n Please enter element name:");
        scanf("%s", name);

        printf("\n Please enter element symbol:");
        scanf("%s", symbol);

        printf("\n Please enter atomic weight:");
        scanf("%f", &atom_weight);


        //printf("%s, %s,...Weight %f",name, symbol, atom_weight);
        table = insert_top(name, symbol, atom_weight, table);

        printf("\nDo you want to continue (Y=1/N=0)? ");
        scanf("%d", &choice); //You should add the space before %c, not after

    }
    printf("Out of cycle\n");
    
    for (element *e = table; e; e = e->next) {
        printf("\nElement: %s \t\t Symbol: %s  \t\t Atomic weight: %f\n",
               e->name, e->symbol, e->atom_weight);
    }          
    return 0;  
}
#包括
#包括
#包括
类型定义结构元素{
字符名[20];
字符符号[20];
浮动原子重量;
结构元素*next;
}元素;
/*将新节点添加到列表顶部*/
元素*插入顶部(字符名称[20],字符符号[20],浮动原子重量,元素*头部){
元素*新元素=malloc(sizeof(元素));
strcpy(新元素->名称、名称);
strcpy(新元素->符号,符号);
新元素->原子重量=原子重量;
新建元素->下一步=头部;
printf(“顶部插入”);
返回新的_元素;
}
int main()
{
元素*表=空;
int-choice=1;
printf(“%d”,选项);
while(选项!=0){
字符名[256];
字符符号[256];
浮动原子重量;
printf(“\n请输入元素名称:”);
scanf(“%s”,名称);
printf(“\n请输入元素符号:”);
扫描频率(“%s”,符号);
printf(“\n请输入原子量:”);
scanf(“%f”&原子重量);
//printf(“%s,%s,…重量%f”,名称、符号、原子重量);
表=插入顶部(名称、符号、原子重量、表);
printf(“\N是否继续(Y=1/N=0)?”;
scanf(“%d”,&choice);//您应该在%c之前添加空格,而不是之后添加空格
}
printf(“周期外\n”);
对于(元素*e=表格;e;e=e->next){
printf(“\n元素:%s\t\t符号:%s\t\t原子量:%f\n”,
e->name,e->symbol,e->atom\u weight);
}          
返回0;
}

这里有很多东西没有意义。你应该仔细阅读任何给定C教科书中关于指针和数组的章节。在
main()
中,你从不将
表的值设置为
NULL
以外的任何值。考虑到你在
main()末尾的
while
循环中如何使用它
我预计那里会有一个segfault。另外,如果您从未递增
I
,您可以将
表[I]
中的所有
替换为
表->
,而
循环位于
main()的底部
。从不初始化的
temp
的意义是什么。我认为您希望将其设置为
table
,然后在
中使用
temp
,而不是
table
而不是
循环中的
table
。C区分大小写
元素
元素
不是一回事