1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

segmentation fault

Discussion in 'C' started by ganesh_don, Mar 9, 2007.

  1. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.....
     
    Last edited by a moderator: Mar 10, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    See my reply here.
     

Share This Page