0

I'm trying to reverse this linked-list but after giving input it gives output Segmentation fault (core dumped) while printing the list. This only happened when I declare 4th pointer before that it was working fine when there were only three pointers head, newcode, temp.

#include <stdio.h>
#include <stdlib.h>

int main()
    {
        struct node
        {
            int data;
            struct node *next;
        };

    struct node *head, *newnode, *temp;
    int choice = 1;

    // Creating a linked-list
    while (choice)
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        printf("Enter the data: ");
        scanf("%d", &newnode->data);
        newnode->next = 0;
        if (head == 0)
        {
            head = temp = newnode;
        }
        else
        {
            temp->next = newnode;
            temp = newnode;
        }
        printf("Do you want to continue: ");
        scanf("%d", &choice);
    }
    temp = head;

    // Reversing the LL
    struct node *prevnode, *currentnode, *nextnode;
    prevnode = 0;
    currentnode = nextnode = head;
    while (nextnode != 0)
    {
        nextnode = nextnode->next;
        currentnode->next = prevnode;
        prevnode = currentnode;
        currentnode = nextnode;
    }
    head = prevnode;

    // Printing the Linked-list
    while (prevnode != 0)
    {
        printf("%d ", prevnode->data);
        prevnode = prevnode->next;
    }
    return 0;
}

I understand segmentation fault error still can't figure which part is actually causing the error.

5
  • 2
    Stepping through the code with a debugger to find exactly where a segmentation fault occurs is a very useful skill to learn. Commented Nov 22, 2021 at 4:08
  • 2
    head is uninitialized, so if (head == 0){ .. } else { ... } and beyond is undefined behavior. Commented Nov 22, 2021 at 4:15
  • The lack of error handling and unclear prompts leads to infinite loops if you don't enter numbers of data and do you want to continue. What data do you enter to triggger the error? I tried 1, 2, 3, 4, 5 and I could not reproduce the segfault. Commented Nov 22, 2021 at 4:16
  • Adding to @sj95126's comment: I have some gdb debugger example notes here you may find useful, for stepping through your code to debug. Search that doc for "GDB USAGE" and "GDB debugging steps example". See also my related Q&A here: AskUbuntu: Where do I find core dump files, and how do I view and analyze the backtrace (stack trace) in one?. Commented Nov 22, 2021 at 4:29
  • in else you need to check like else{ while(temp->next !=NULL){ temp=temp->next;}} Commented Nov 22, 2021 at 4:33

2 Answers 2

1

You need to initialize head to 0. Pointers are not initialized by default. Otherwise, it works fine.

Sign up to request clarification or add additional context in comments.

4 Comments

But i still don't understand without initializing head to 0 code was still working fine if i printed without reversing. is it possible?
The only problem was that head was not assigned in the first loop, since the condition head == 0 was never true. The list however was built OK. In the version you say works you probably didn't use the head from the first loop.
When you started walking the list from head (that had a random value) you accessed an unallocated part of the memory, hence the error. However, the memory allocation for the list was OK, only that head did not point to the first element.
@AdamMackie using an uninitialized value is undefined behavior. It may work sometimes, not other times, it's fruitless to try to understand. This is a list of common UB in C, you are guilty of #5.
1

head is uninitialized, because pointer aren't initialized by default

1 Comment

this worked man! there were also other codes which were giving me same error but now they all are working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.