Like Tree1Likes
  • 1 Post By ZuK

ERROR: segmentation fault (core dumped)

This is a discussion on ERROR: segmentation fault (core dumped) within the C Programming forums, part of the General Programming Boards category; when i am trying to run this program on ubuntu it compiles but when i run it it flags as ...

  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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ZuK
    ZuK is offline
    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
    rags_to_riches likes this.

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, 06:14 AM

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