peculiar behaviour

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

  1. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    1.) #include<stdio.h>
    2.) #include<malloc.h>

    3.) struct node
    4.) {
    5.) int val;
    6.) struct node *next;
    7.) };

    8.) typedef struct node *Node;

    9.) struct stack
    10.) {
    11.) Node head;
    12.) };

    13.) typedef struct stack *Stack;

    14.) int main()
    15.) {
    16.) Stack s;
    17.) int val;
    18.) s->head=NULL;
    19.) return 0;
    20.) }

    Note: line numbers are provided for easy discussion abt the line

    Buddies....This program gives me segmentation fault but when i interchanged line 16 and 17 it worked fine.. I dont know wat actually happened.somebody plzz help me....thanks in advance
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are not allocating the pointer s and so anything can happen which is out of your control.
     
  3. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Thankyou buddy......i have been breaking my head for days.Thanks a lot dude...it worked fine.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice