Like Tree2Likes
  • 1 Post By stahta01
  • 1 Post By memcpy

Segmentation Fault (core dumped)

This is a discussion on Segmentation Fault (core dumped) within the C Programming forums, part of the General Programming Boards category; i am new to this language kindly can anyone help me with this code. Code: #include<stdio.h> struct node{ int data; ...

  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
    2,613
    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.
    rags_to_riches likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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.
    Salem likes this.

  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, 02: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, 02:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21