C-创建带字符串的链表
我有一个存储学生信息的链表。该列表包括一个C-创建带字符串的链表,c,data-structures,linked-list,C,Data Structures,Linked List,我有一个存储学生信息的链表。该列表包括一个char*和两个int,当我试图用字符串初始化我的char*时,问题就出现了,因为我这样做时会出现Seg错误 我尝试过的事情:使用strcpy而不是=以及动态分配内存 typedef struct Student{ int age,id; char *name; struct Student *next; }Student; struct Student *head = NULL; 创建链表函数 Student *create
char*
和两个int
,当我试图用字符串初始化我的char*
时,问题就出现了,因为我这样做时会出现Seg错误
我尝试过的事情:使用strcpy而不是=
以及动态分配内存
typedef struct Student{
int age,id;
char *name;
struct Student *next;
}Student;
struct Student *head = NULL;
创建链表函数
Student *create (){
char name[128];
Student *newStudent = (Student*)malloc(sizeof(Student));
newStudent->name = malloc(sizeof(Student) + 1);
newStudent->age = (rand() % (35 - 18 + 1)) + 18; // age range of student from 18-35
newStudent->id = rand() % 1000000 + 100000; // 6 digit id
scanf("%127s", name);
strcpy(newStudent->name, name);
newStudent->next = NULL;
return newStudent;
}
Student *insert(Student *newStudent){
Student *ptr = head;
if (head == NULL)
return newStudent;
while (ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = newStudent;
return head;
}
插入函数
Student *create (){
char name[128];
Student *newStudent = (Student*)malloc(sizeof(Student));
newStudent->name = malloc(sizeof(Student) + 1);
newStudent->age = (rand() % (35 - 18 + 1)) + 18; // age range of student from 18-35
newStudent->id = rand() % 1000000 + 100000; // 6 digit id
scanf("%127s", name);
strcpy(newStudent->name, name);
newStudent->next = NULL;
return newStudent;
}
Student *insert(Student *newStudent){
Student *ptr = head;
if (head == NULL)
return newStudent;
while (ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = newStudent;
return head;
}
构建函数
Student *build(){
int size;
Student *newStudent = (Student*)malloc(sizeof(Student));
printf("Enter size of linked list: ");
scanf("%d",&size);
for (int i=0; i<size; i++){
newStudent = create();
head = insert(newStudent);
}
return head;
}
char*名称;斯特伦(姓名)
是未定义的行为,segfault是合理的期望。类似于char*name;scanf(“%s”,名称)代码>
在这两种情况下,name
都是未初始化的(因此您可以将其视为无处寻址)。当您尝试在无处计算字符串的长度时,会出现错误。类似地,当您尝试使用scanf
将一些数据写入到任何地方时。也许你想要:
char name[128]; scanf("%127s", name);
或类似的。无论您做什么,都需要name
在使用之前引用有效的内存位置。无论您是将其声明为数组还是将其声明为指针并使用malloc
为其分配地址,这都无关紧要。(使其成为一个数组通常更容易,因为您不需要担心释放它);斯特伦(姓名)
是未定义的行为,segfault是合理的期望。类似于char*name;scanf(“%s”,名称)代码>
在这两种情况下,name
都是未初始化的(因此您可以将其视为无处寻址)。当您尝试在无处计算字符串的长度时,会出现错误。类似地,当您尝试使用scanf
将一些数据写入到任何地方时。也许你想要:
char name[128]; scanf("%127s", name);
或类似的。无论您做什么,都需要name
在使用之前引用有效的内存位置。无论您是将其声明为数组还是将其声明为指针并使用malloc
为其分配地址,这都无关紧要。(使其成为一个数组通常更容易,因为您不需要担心释放它)。在这种情况下,您似乎不应该使用union
作为名称和年龄/id字段。不会分别存储所有三个成员。相反,它将在一个位置一次存储一个成员。因此,当您设置u.age
时,您将删除u.name
,反之亦然。在这种情况下,您不应该对name和age/id字段使用union
。不会分别存储所有三个成员。相反,它将在一个位置一次存储一个成员。因此,当您设置u.age
时,您将删除u.name
,反之亦然。我修复了我的代码,包括您的输入和关于删除union
的注释,但仍然会出现seg错误。现在,您在strcpy(newStudent->name,name)方面也有同样的问题
您尚未初始化newStudent->name
,因此尝试将数据复制到该地址无效。请尝试另一个malloc
,或struct Student{int age,id;char name[128];struct Student*next;}
谢谢!在分配了newStudent->name
之后,我让它开始工作。我修复了我的代码,包括您的输入和关于删除union
的注释,但我仍然会遇到seg错误。现在您在strcpy(newStudent->name,name)方面也有同样的问题
您尚未初始化newStudent->name
,因此尝试将数据复制到该地址无效。请尝试另一个malloc
,或struct Student{int age,id;char name[128];struct Student*next;}
谢谢!在分配了newStudent->name
之后,我让它开始工作。