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.
void printNode(struct ListNode *l){
// Prints the each value of the struct, separated by tab.
if ((l->next) != NULL){
printf("%d\t",(l->val));
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.
if ((l->next) != NULL){
printf("%d\t",(l->val));
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.
CodePudding user response:
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);
}
Also, your printNode()
doesn't look right to me. Try this instead:
void printNode(struct ListNode *l) {
if(!l) return;
printf("%d\t", l->val);
printNode(l->next);
}
Here is a streamlined version of makeNodeFromList()
:
void makeNodeFromList(int length, int arr[length], struct ListNode nodes[length]){
for(size_t i = 0; i < length; i ) {
nodes[i].val = arr[i];
nodes[i].next = i 1 < length ? &nodes[i 1] : NULL;
}
}
int main() {
int arr[] = {1,2,3};
struct ListNode nodes[sizeof arr / sizeof *arr];
makeNodeFromList(sizeof arr / sizeof *arr, arr, nodes);
printNode(nodes);
}