#include<stdio.h>
#include<malloc.h>
typedef struct Node {
int data;
struct Node * next;
} Node;
void push(Node **headRef, int i){
//why does headRef == NULL in below if condition gives segmentation fault?
if(*headRef == NULL){
*headRef = malloc(sizeof(Node));
Node *head = *headRef;
head->data = i;
}
}
int main(int argc, char ** argv){
Node *head = NULL;
push(&head, 2);
printf("%d\n", head->data);
}
此代码是链接列表,我尝试将某些数据推送到列表中。 我的问题是在推送功能的评论。等式双指针为NULL给出了C中的分段错误
我没有看到该行的任何错误。 – NPE 2012-01-28 12:34:10
在标准C中,'malloc'生活在''中,而不是''。我无法重现错误。 –
2012-01-28 12:36:42
除此之外,您的代码运行良好。你的问题必须在其他地方。 – 2012-01-28 12:37:37