Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am i getting Segmentation fault (core dumped) when i try to print a struct using recursion?

I am new to C. I am trying to make a linked list with the following structure.

struct ListNode {
    int val;
    struct ListNode *next;
};

So i am using recursion to create the linked list from a given array.

struct ListNode makeNode(int val, struct ListNode* l){
    struct ListNode node;
    node.val = val;
    node.next = l;
    return node;
}

struct ListNode makeNodeFromList(int arr[], int length, int index){

    // Checks if the given index is the last element of the array
    // (Base Condition)
    if ((length-1) == index){
        return makeNode(arr[index], NULL);
    }

    struct ListNode next;
    next = makeNodeFromList(arr, length, index+1);
    return makeNode(arr[index],&next);

}

But when i try to print the result using the following code it keeps printing the second element and i get Segmentation fault (core dumped) error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void printNode(struct ListNode *l){
    // Prints the each value of the struct, separated by tab.
    printf("%d\t",(l->val));

    if ((l->next) != NULL){
        printNode((l->next));
    }
    else{
        printf("%d\n",(l->val));
    }
}

int main() { 
    int arr[3] = {1,2,3};
    struct ListNode item;
    item = makeNodeFromList(arr,3,0);
    printNode(&item);
    }

*-Edit

The whole code instead of snippets.

#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode makeNode(int val, struct ListNode* l){
    struct ListNode node;
    node.val = val;
    node.next = l;
    return node;
}

struct ListNode makeNodeFromList(int arr[], int length, int index){
    printf("%d\t", arr[index]);

    // Checks if the given index is the last element of the array.
    if ((length-1) == index){
        return makeNode(arr[index], NULL);
    }

    struct ListNode next;
    next = makeNodeFromList(arr, length, index+1);
    return makeNode(arr[index],&next);

}

void printNode(struct ListNode *l){
    // Prints the each value of the struct, separated by tab.
    printf("%d\t",(l->val));

    if ((l->next) != NULL){
        printNode((l->next));
    }
    else{
        printf("%d\n",(l->val));
    }
}

int main() { 
    int arr[3] = {1,2,3};
    struct ListNode item;
    item = makeNodeFromList(arr,3,0);
    printNode(&item);

    }

I cant figure out the mistake i made.

I tried to find similar problems but couldn’t find a similar case.

>Solution :

Running you program under gdb, I find that your program segfaults in printf("%d\t",(l->val));. The variable l->next cannot be accessed. As @Someprogrammerdude told you above that it is because it’s a local variable next in makeNodeFromList() is out of scope when it returns.

The minimal fix is (and you need a matching free):

#include <string.h>

struct ListNode makeNodeFromList(int arr[], int length, int index){
    if ((length-1) == index) {
        return makeNode(arr[index], NULL);
    }
    struct ListNode next = makeNodeFromList(arr, length, index+1);
    struct ListNode *p = malloc(sizeof(next));
    memcpy(p, &next, sizeof(next));
    return makeNode(arr[index], p);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading