0

I have written a similar piece of code in C++ for Windows where I create a basic singly linked list, add data and display the list's content. I tried writing a similar kinda program in C this time for Linux. There seems to be no compiler error or run-time error, but when I try to call the function void insert() , the program console tells me there is a segmentation error.

My code is included below :

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

typedef struct Node
{
    int data;
    struct Node* next;
}*nPtr;

nPtr head = NULL;
nPtr cur = NULL;
void insert(int Data);
void display();

int main(void)
{
    int opr, data;

    while (opr != 3)
    {
        printf("Choose operation on List. \n\n1. New Node. \n2. Display List.\n\n>>>");
        scanf("%d", opr);

        switch (opr)
        {
            case 1 :
                printf("Enter data.\n");
                scanf("%d", data);

                insert(data);
                break;

            case 2 :
                display();
                break;

            case 3 :
                exit(0);

            default :
                printf("Invalid value.");
        }
    }

    getchar();
}

void insert(int Data)
{
    nPtr n = (nPtr) malloc(sizeof(nPtr));

    if (n == NULL)
    {
        printf("Empty List.\n");
    }

    n->data = Data;
    n->next = NULL;

    if(head != NULL)
    {
        cur= head;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = n;
    }
    else
    {
        head = n;
    }
}

void display()
{
    struct Node* n;

    system("clear");
    printf("List contains : \n\n");

    while(n != NULL)
    {
        printf("\t->%d", n->data, "\n");
        n = n->next;
    }
}

When I run the code, there doesn't seem any problem or error at all. But when I call either of the 2 functions I created up there, there is an error which says "Segmentation fault". I'd assume something would be wrong with my malloc() function in void insert(), but I can't pin-point what's wrong in the void display() method.

  • There's no reason for `cur` to be a global variable, it should be local to `insert()`. – Barmar Jan 28 '20 at 18:20
  • [is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Barmar Jan 28 '20 at 18:21
  • [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Jan 28 '20 at 18:21
  • 1
    Take a look at [scanf()](https://en.cppreference.com/w/c/io/fscanf) before using it. – H.S. Jan 28 '20 at 18:24
  • In `insert()`, this `nPtr n = (nPtr) malloc(sizeof(nPtr));` is wrong. It should be `nPtr n = malloc(sizeof(struct Node));` – H.S. Jan 28 '20 at 18:28
  • *"There seems to be no compiler error or run-time error..."* -- enable compiler warnings (`/W3` for VS, or `-Wall -Wextra -pedantic` for gcc/clang) and do not accept code until it compiles without warning. – David C. Rankin Jan 28 '20 at 18:36

1 Answers1

1

The display() function never initializes n. The declaration should be:

nPtr n = head;
Barmar
  • 741,623
  • 53
  • 500
  • 612