Thread: Segmentation Fault (core dumped)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Segmentation Fault (core dumped)

    i am new to this language kindly can anyone help me with this code.

    Code:
    #include<stdio.h>
    struct node{
    int data;
    struct node *lft;
    struct node *rgt;
    };
    
    struct node * cons(struct node *p,int a)
    {if(p==NULL)
    {p=(struct node *)malloc(sizeof(struct node));
    p->data=a;
    p->lft=NULL;
    p->rgt=NULL;
    }
    else
    {if(a<p->data)
    p->lft=cons(p->lft,a);
    else if(a>p->data)
    p->rgt=cons(p->rgt,a);
    }
    return p;
    }
    
    int preorder(struct node *p)
    {struct node *q;
    q=(struct node *)malloc(sizeof(struct node));
    q=p;
    printf("%d",q->data);
    preorder(q->lft);
    preorder(q->rgt);
    return 0;
    }
    
    int main()
    {int num;struct node *p;
    p=NULL;
    while(num!=0)
    {scanf("%d",&num);
    p=cons(p,num);
    }
    preorder(p);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    IMHO, Case book example of this FAQ
    FAQ > Casting malloc - Cprogramming.com
    from above link
    There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included.
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Your indentation is actually awful.

    The problem is, however, that in the function "preorder" line 27, the equals statement should be the other way around. Change "q=p" to "p=q".

    That's not to say that the program works as expected: look at the (fixed) output and tell me if the infinite loop of allocating and printing zeroes is what you want.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you want to copy the contents of p into q use memcpy().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  2. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  3. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread