Search code examples
cdata-structureslinked-listprogram-entry-point

C - Not working (node) structures in main function (intentionally avoiding use of singly linked list implementation)


I'm trying to create a singly linked list without implementation. I'm doing this just to gain some deeper understanding on how structures work. I just want to create 2 nodes in the main function and link them. I could do that using typedef in the declaration of structures and by making the implementation, but that's not what I want (I can do that successfully).

I've written some code, but an error occurs at line 27 (the same problem appears at lines 29, 30 and 33). I know the explanation for this problem is rather simple, but I couldn't find it, either on my mind or on the web (or books). I'd appreciate any assistance in this problem. I've been programming just for the last year.

I thought in asking for help in CodeReview, but accordingly, to their FAQ, that wasn't the place to ask for help with this kind of problem.

Errors are:

27:11: error: incompatible types when assigning to type 'struct NODO ' from type 'struct NODO'

E.sig = F;

Same problem with the other lines. Everything is in the same file (.c).

Here's the full code:

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

struct NODE {
    int data;
    struct NODE *next;
};

struct LIST {
    struct NODE *first;
    struct NODE *last;
    int size;
};


void main(){

    struct NODE E;
    struct NODE F;
    struct LIST L;

    E.data = 1;
    E.next = NULL;

    F.data = 2;
    F.next = NULL;
    E.next = F;          // line 27

    L.first = E;         // line 29
    L.last = F;          // line 30

    struct NODE *ptr;
    ptr = E;             // line 33

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

Solution

  • The problem is that you are trying to assign an object to a pointer. You should assign the address of your node and note the node object itself.

    Try E.next = &F; and same for all the others.