PDA

View Full Version : Help with Segmentation Fault! Simple Code.



ragedmonkey
May 30th, 2011, 11:33 PM
Hello, this is my simple attempt to start implementing Linked List.
I am getting a Segmentation Fault at
Line:
newNode->item =d;

Does anyone know why?



#include <assert.h>
#include <sys/types.h>
#include <stdio.h>

struct node{
int item;
struct node *next;
};

struct node* createNode(int d){
struct node *newNode;
newNode->item = d;
//newNode->next = NULL;
return newNode;
}

int main(){
struct node *new = createNode(10);
//int x=0;
//printf("%d\n",new->item);
return 0;
}

slavik
May 30th, 2011, 11:44 PM
because new node is being allocated on the stack of the createNode function, read about malloc() and don't miss class.

ragedmonkey
May 30th, 2011, 11:48 PM
Thanks for your response!.Problem Solved.