Thread: ERROR: segmentation fault (core dumped)

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

    Unhappy ERROR: segmentation fault (core dumped)

    when i am trying to run this program on ubuntu it compiles but when i run it it flags as "segmentation fault (core dumped)". can anyone tell me how to get the output.the code is as follows:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node{
    int info;
    struct node*link;
    };
    void append(struct node**, int);
    void displ(struct node*);
    
    
    
    
    void append(struct node **q, int n){
     struct node *temp,*r;
    
    
    if(*q==NULL){
    printf("the list has zero elements and the first node being added is %d\n",n);
    temp->info=n;
    temp->link=NULL;
    *q=temp;
     }
    else
    {temp=*q;
      while(temp->link!=NULL)
          temp=temp->link;
    
    
    r=malloc(sizeof(struct node));
    r->info=n;
    r->link=NULL;
    temp->link=r;
    }
    
    
    }
    
    
    void displ(struct node *q){
    struct node *p;
    for(p=q;p->link!=NULL;p=p->link)
     printf("%d",p->info);
    
    
    }
    
    
     main(){
    struct node *p;
    int n;
    p=NULL;
    printf("enter value to append a node\n");
    scanf("%d",&n);
    //append(&p,12);
    displ(p);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to format your code better, especially the indentation:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct node {
        int info;
        struct node *link;
    };
    
    void append(struct node**, int);
    void displ(struct node*);
    
    void append(struct node **q, int n) {
        struct node *temp, *r;
    
        if (*q == NULL) {
            printf("the list has zero elements and the first node being added is %d\n", n);
            temp->info = n;
            temp->link = NULL;
            *q = temp;
        }
        else {
            temp = *q;
            while (temp->link != NULL)
                temp = temp->link;
            r = malloc(sizeof(struct node));
            r->info = n;
            r->link = NULL;
            temp->link = r;
        }
    }
    
    void displ(struct node *q) {
        struct node *p;
        for (p = q; p->link != NULL; p = p->link)
            printf("%d", p->info);
    }
    
    main() {
        struct node *p;
        int n;
        p = NULL;
        printf("enter value to append a node\n");
        scanf("%d", &n);
        //append(&p,12);
        displ(p);
    }
    Next, one problem you have is that when inserting the first node, you forgot to allocate memory for it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You're passing NULL for q

    Code:
    void displ(struct node *q){
        struct node *p;
        for(p=q;p->link!=NULL;p=p->link) // <<== boom
             printf("%d",p->info); // <<= boom
    }
    Running the program in the debugger helps a lot to find this kind of errors.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-18-2012, 06:27 AM
  2. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  3. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM