在 C 中操作链表时如何解决这个段错误问题?
How can I fix this segfault issue when manipulating linked-lists in C?
我一直在研究自引用结构和链表,特别是用于 C 作业。问题是,当我尝试取消引用指向列表头部的指针时,我得到了段错误或 "assignment from incompatible pointer type".
我查看了 this explanation 关于操作链表的内容,并尝试按照他们的示例来取消引用头部,但是当我这样做时我总是遇到段错误。
我也用过onlinegdb调试过,段错误来自:
new_node->next = (*h);
(*h) = new_node;
crawler = (*head_of_list);
所以我试图避免取消引用,它停止了段错误但仍然无法正常工作。它认为取消引用列表的头部就是这一切的全部内容,所以这里有点混乱。
#include <stdio.h>
#include <stdlib.h>
/*DATA STRUCTURES*/
typedef struct Node_s* Ptr_Node; /*Ptr_Node = pointer to struct node_s*/
typedef struct Node_s{
int data;
Ptr_Node next;
} Node_t;
typedef Ptr_Node* Head; /*a head = pointer to pointer of node*/
/*FUNCTIONS*/
/*Adds integer x in first position of the linked list*/
Head add_to_beginning(Head head_of_list, int x){
Head h;
h = head_of_list;
Ptr_Node new_node;
new_node = (Ptr_Node) malloc(sizeof(Node_t)); /*casting pointer type*/
new_node->data = x;
new_node->next = (*h); /*de-ref head to obtain pointer to first node*/
(*h) = new_node; /*de-ref head to change what it points to*/
return h;
}
void print_list(Head head_of_list){
Ptr_Node crawler;
crawler = (*head_of_list); /*points to first cell of list*/
while(crawler != NULL){
printf("%d\n", crawler->data );
crawler = crawler->next;
}
}
/*driver for testing*/
int main(void){
Head h0, h1, h2;
h0 = NULL;
h1 = add_to_beginning(h0, 0);
h2 = add_to_beginning(h1, 1);
h3 = add_to_beginning(h2, 2);
print_list(head_of_list);
return 0;
}
非常感谢任何关于如何解决这个问题的帮助。
你定义Head指针为0:
h0 = NULL;
然后你在这里取消引用它:
new_node->next = (*h);
指针指向地址零,你得到一个段错误。确保它指向有效内存:)
我一直在研究自引用结构和链表,特别是用于 C 作业。问题是,当我尝试取消引用指向列表头部的指针时,我得到了段错误或 "assignment from incompatible pointer type".
我查看了 this explanation 关于操作链表的内容,并尝试按照他们的示例来取消引用头部,但是当我这样做时我总是遇到段错误。 我也用过onlinegdb调试过,段错误来自:
new_node->next = (*h);
(*h) = new_node;
crawler = (*head_of_list);
所以我试图避免取消引用,它停止了段错误但仍然无法正常工作。它认为取消引用列表的头部就是这一切的全部内容,所以这里有点混乱。
#include <stdio.h>
#include <stdlib.h>
/*DATA STRUCTURES*/
typedef struct Node_s* Ptr_Node; /*Ptr_Node = pointer to struct node_s*/
typedef struct Node_s{
int data;
Ptr_Node next;
} Node_t;
typedef Ptr_Node* Head; /*a head = pointer to pointer of node*/
/*FUNCTIONS*/
/*Adds integer x in first position of the linked list*/
Head add_to_beginning(Head head_of_list, int x){
Head h;
h = head_of_list;
Ptr_Node new_node;
new_node = (Ptr_Node) malloc(sizeof(Node_t)); /*casting pointer type*/
new_node->data = x;
new_node->next = (*h); /*de-ref head to obtain pointer to first node*/
(*h) = new_node; /*de-ref head to change what it points to*/
return h;
}
void print_list(Head head_of_list){
Ptr_Node crawler;
crawler = (*head_of_list); /*points to first cell of list*/
while(crawler != NULL){
printf("%d\n", crawler->data );
crawler = crawler->next;
}
}
/*driver for testing*/
int main(void){
Head h0, h1, h2;
h0 = NULL;
h1 = add_to_beginning(h0, 0);
h2 = add_to_beginning(h1, 1);
h3 = add_to_beginning(h2, 2);
print_list(head_of_list);
return 0;
}
非常感谢任何关于如何解决这个问题的帮助。
你定义Head指针为0:
h0 = NULL;
然后你在这里取消引用它:
new_node->next = (*h);
指针指向地址零,你得到一个段错误。确保它指向有效内存:)