Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   segmentation fault (http://www.go4expert.com/forums/segmentation-fault-t3331/)

ganesh_don 10Mar2007 03:14

segmentation fault
 
Code:

#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct node
{
  char *data;
  struct node *next;
};

typedef struct node *Node;

struct stack
{
  Node head;
 
};

typedef struct stack *Stack;

Node createNode(char *data)
{
  Node newnode;
  newnode=(Node)malloc(sizeof(Node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}

void push(Stack s,char *str)
{
  Node newnode;
  newnode=createNode(str);
  if(!newnode)
  {
        printf("Memory exhausted\n");
        return;
  }
  newnode->next=s->head;
  s->head=newnode;       
}


int main()
{
Stack s;
char *str;
s->head=NULL;
push(s,"hai");
return 0;
}

Actually its a part of stack implimentaion.Dudes.... when i try to initilise the head to NULL as in the main() or if i try to assign it to some other reference as in the push module its giving me segmentation fault.I dont know wats happening here can anyone cud kindly explain why this program went wrong.Iam really confused ill be greatful if u ppl help me.......sorry for the long post.....thanks in advance.....

DaWei 10Mar2007 06:17

Re: segmentation fault
 
See my reply here.


All times are GMT +5.5. The time now is 02:23.