gpt4 book ai didi

c - 如何修复打印链表时的段错误?

转载 作者:行者123 更新时间:2023-11-30 19:00:58 27 4
gpt4 key购买 nike

当尝试打印我的学生链接列表时,我收到“段错误(核心转储)”。当我通过硬编码打印列表时:

`

for (i = 0; i < 3; i++){
printf("%s, %s\n", head->pStudent->last, head->pStudent->first);
head = head->next;
}

`

(创建了 3 个节点。)它有效!当我尝试像这样打印它时:

`

while (head != NULL){
printf("%s, %s\n", head->pStudent->last, head->pStudent->first);
head = head->next;
}

`

它给了我段错误。

我尝试过while (head->next != NULL){...}

我尝试在 main 中执行此操作,而不是调用 print_list(head);


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

typedef struct student{
char *first;
char *last;
} student_t;

typedef struct node{
student_t *pStudent;
struct node *next;
} node_t;

void addToStart(node_t ** head, student_t *student);
void print_list (node_t * head);
student_t *newStudent();

int main(){
node_t *head;
student_t *studentOne = (student_t *) malloc(sizeof(student_t));
student_t *studentTwo = (student_t *) malloc(sizeof(student_t));
student_t *studentThree = (student_t *) malloc(sizeof(student_t));

studentOne->first = (char *) malloc(sizeof(char));
studentOne->last = (char *) malloc(sizeof(char));

studentTwo->first = (char *) malloc(sizeof(char));
studentTwo->last = (char *) malloc(sizeof(char));

studentThree->first = (char *) malloc(sizeof(char));
studentThree->last = (char *) malloc(sizeof(char));

studentOne = newStudent();
addToStart(&head, studentOne);

studentTwo = newStudent();
addToStart(&head, studentTwo);

studentThree = newStudent();
addToStart(&head, studentThree);

print_list(head);

printf("Then you will enter 3 students names that will be added to the end of the list\n");

return 0;
}

void addToStart(node_t ** head, student_t *student){
node_t *new = (node_t *) malloc(sizeof(node_t));

new->pStudent = student;

new->next = *(head);
*(head) = new;
}

void print_list (node_t * head){
while (head != NULL){
printf("%s, %s\n", head->pStudent->last, head->pStudent->first);

head = head->next;
}

}

student_t *newStudent(){
student_t *new = (student_t *) malloc(sizeof(student_t));
new->first = (char *) malloc(sizeof(char));
new->last = (char *) malloc(sizeof(char));

printf("Enter first name of student: ");
scanf("%s", new->first);
printf("Enter last name of student: ");
scanf("%s", new->last);

return new;
}

预期结果:


Enter first name of student: 1
Enter last name of student: 1
Enter first name of student: 2
Enter last name of student: 2
Enter first name of student: 3
Enter last name of student: 3
3, 3
2, 2
1, 1

实际结果:


Enter first name of student: 1
Enter last name of student: 1
Enter first name of student: 2
Enter last name of student: 2
Enter first name of student: 3
Enter last name of student: 3
3, 3
2, 2
1, 1
Segmentation fault (core dumped)

最佳答案

按照此代码的方式,列表中的最后一个节点将指向您第一次执行 addToStart 时的 head 变量。但 head 未初始化:

node_t *head;

正确初始化它,以便循环正确终止:

node_t *head = NULL;

您的字符串也只有 1 个 char 的内存。除了空字符串之外,这还不够。试试这个:

new->first = malloc(32); // memory for 31 chars plus null terminator
new->last = malloc(32);

printf("Enter first name of student: ");
scanf("%31s", new->first); // read a maximum of 31 chars
printf("Enter last name of student: ");
scanf("%31s", new->last);

此外,您为学生和字符串分配了两次内存,从而导致进程中内存泄漏。删除这些冗余并执行以下操作:

student_t *studentOne = newStudent();
student_t *studentTwo = newStudent();
student_t *studentThree = newStudent();

addToStart(&head, studentOne);
addToStart(&head, studentTwo);
addToStart(&head, studentThree);

您也没有在任何地方释放任何内存,所以我假设您稍后将编写一个函数来清理列表。经过这些更改,程序如下所示:

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

typedef struct student {
char *first;
char *last;
} student_t;

typedef struct node {
student_t *pStudent;
struct node *next;
} node_t;

void addToStart(node_t ** head, student_t *student);
void print_list(node_t * head);
student_t *newStudent();

int main() {
node_t *head = NULL;
student_t *studentOne = newStudent();
student_t *studentTwo = newStudent();
student_t *studentThree = newStudent();

addToStart(&head, studentOne);
addToStart(&head, studentTwo);
addToStart(&head, studentThree);

print_list(head);

printf("Then you will enter 3 students names that will be added to the end of the list\n");

return 0;
}

void addToStart(node_t ** head, student_t *student) {
node_t *new = (node_t *)malloc(sizeof(node_t));

new->pStudent = student;

new->next = *(head);
*(head) = new;
}

void print_list(node_t * head) {
while (head != NULL) {
printf("%s, %s\n", head->pStudent->last, head->pStudent->first);

head = head->next;
}

}

student_t *newStudent() {
student_t *new = (student_t *)malloc(sizeof(student_t));
new->first = (char *)malloc(32);
new->last = (char *)malloc(32);

printf("Enter first name of student: ");
scanf("%31s", new->first);
printf("Enter last name of student: ");
scanf("%31s", new->last);

return new;
}

关于c - 如何修复打印链表时的段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com