Arlind
Arlind

Reputation: 434

Linked List error - C

I'm just messing around with linked lists but it seems I have many things wrong in my mind. I've tried this with functions and I couldn't get it working. I thought I'd get it right by making it simplier but it still doesn't work. It usually prints just the first element right and then random huge numbers (memory addresses most likely)

I just want to create a list and print its content.

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

struct el{
    int value;
    struct el *next;
};

typedef struct el Elem;



int main()
{
    int nr, i;
    struct el *Head, *Conductor;
    Head = malloc(sizeof(Elem));
    Conductor = Head;
    printf("How many elements do you want to add? ");
    scanf("%d", &nr);
    for(i = 0; i < nr; i++)
    {
        printf("Enter value for element %d:  ", i);
        scanf("%d", &(Conductor->value));
        Conductor = Conductor->next;
        Conductor = malloc(sizeof(Elem));


    }
    free(Conductor->next);
    Conductor->next = NULL;

    Conductor = Head;
    printf("\n");

    for(i = 0; i < nr; i++)
    {
        printf("%d ->  ", Conductor->value);
        Conductor = Conductor->next;
    }

   return 0;
}

Upvotes: 0

Views: 120

Answers (4)

sirius
sirius

Reputation: 1039

You should allocate memory for Conductor->next before assigning Conductor->next to Conductor.

for(i = 0; i < nr; i++)
{
    printf("Enter value for element %d:  ", i);
    scanf("%d", &(Conductor->value));
    Conductor->next = malloc(sizeof(Elem));
    Conductor = Conductor->next;
}

It's important that you make the allocation before the assignment because otherwise the "old" Conductor->next will not point to the new Conductor.

Upvotes: 6

Nobilis
Nobilis

Reputation: 7448

Right here: Conductor = Conductor->next;

Conductor is assigned a NULL value, so when you try to dereference it to store input value in the next iteration of the loop you get a segmentation fault.

As others have pointed out you should first allocate memory for Conductor->next using malloc.

Can I finally recommend that you have a look at a debugger such as gdb as it makes identifying such problems a breeze?

This is how I found out that there was a problem with Conductor->next using gdb:

I stepped through the program until I reached the offending instruction:

25    Conductor = Conductor->next;

I tried to print what's in next:

(gdb) print Conductor->next    

And here's what I got:

Cannot access memory at address 0x4

Whereas this is what I have in Conductor

(gdb) print Conductor
$7 = (struct el *) 0x804b008

Very easy to see we have a problem as in the next loop it will be assigned an invalid value;

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

You do not allocate memory for the next pointer.

You need to allocate before assigning:

scanf("%d", &(Conductor->value));
Conductor->next = malloc(sizeof(Elem));
Conductor = Conductor->next;

Upvotes: 5

Deepu
Deepu

Reputation: 7610

The memory should be allocated to Conductor->next,

Change the loop as follows,

for(i = 0; i < nr; i++)
{
    printf("Enter value for element %d:  ", i);
    scanf("%d", &(Conductor->value));
    Conductor->next = malloc(sizeof(Elem));
    Conductor = Conductor->next;

}

Upvotes: 2

Related Questions