【发布时间】:2016-10-28 18:45:52
【问题描述】:
以下代码在 gcc 编译器中出现分段错误。 早些时候它工作正常,但在更新我的编译器后无法工作。 不知道它在一些在线编译器中发生了什么。
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
//push function to add node at the beginning
void push(struct node** head,int data){
//creating new node
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
//pointing to head if not null
if((*head) != NULL)
newNode->next = (*head);
//making new node as head
(*head) = newNode;
}
//fuction to display linked list
void display(struct node* head){
while(head!=NULL){
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
int main(){
struct node* head;
push(&head,20);
push(&head,10);
push(&head,5);
display(head);
return 0;
}
【问题讨论】:
标签: gcc linked-list segmentation-fault