如何避免链表的分段错误
How to avoid segmentation fault with linked lists
我想编写一个使用链表的代码,从终端获取输入,然后打印出 table。
在这个例子中,我从元素 table 上传一些信息。
我遇到分段错误。
有人可以帮我理解为什么吗?
#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;
}
}
您需要使用 strcpy 复制字符串,请参阅 insert_top
Element
没有定义,应该是element
。
次要不转换 malloc 结果。
还有一些问题,char大小限制,scanf return代码有待检查。
你需要在最后释放 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;
}
我想编写一个使用链表的代码,从终端获取输入,然后打印出 table。
在这个例子中,我从元素 table 上传一些信息。
我遇到分段错误。
有人可以帮我理解为什么吗?
#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;
}
}
您需要使用 strcpy 复制字符串,请参阅 insert_top
Element
没有定义,应该是element
。
次要不转换 malloc 结果。
还有一些问题,char大小限制,scanf return代码有待检查。
你需要在最后释放 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;
}