0

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;
    }
}
carloswm85
  • 1,396
  • 13
  • 23

3 Answers3

4

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.

Benjamin Barrois
  • 2,566
  • 13
  • 30
  • And I think the error message you gave to us misses something like a `&` character. Did you remove it? – Benjamin Barrois Aug 02 '18 at 17:48
  • 1
    More likely to be missing `*` in the error message. – Christian Gibbons Aug 02 '18 at 18:22
  • No missing operand in the message I've copied. I double checked. But, apparently the error message with the mentioned * is done in the last error message, not in the first two error messages. It says: `30:11: error: incompatible types when assigning to type 'struct NODE *' from type 'struct NODE' ` – carloswm85 Aug 02 '18 at 19:10
  • Is it correct to say "object" in C where referring to a structure? @BenjaminBarrois you wrote "trying to assign an object to a pointer". – carloswm85 Aug 02 '18 at 19:22
  • 1
    @CarlosW.Mercado A structure is just a pattern to make objects (or instances if you prefer). You must not confuse structure declaration and implementation (the pattern) `struct X { ... };` and its instantiations `X myX; X myOtherX;`. Here we are talking about binding instances of structures, not structures themselves. – Benjamin Barrois Aug 03 '18 at 07:10
0

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. So you have to provide the address of the memory location .In C address of a variable is derived using & operator.

Modified code :-

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

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

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


int 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;          // not  E.next = F; 

    L.first = &E;         // not  L.first = E;
    L.last = &F;          // not  L.last = F;

    struct NODE *ptr;
    ptr = &E;             // not ptr = E;   

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

    return 0;
}

Recommended to use int main() instead of void main().

Output :-

1
2
anoopknr
  • 3,177
  • 2
  • 23
  • 33
0

I've found the solution or at least some additional information that led me towards the solution minutes after sending the question to SO.

In Wikipedia (https://en.wikipedia.org/wiki/Struct_(C_programming_language)#Pointers_to_struct) I've found the next snippet of code:

struct point {
   int x;
   int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                 and initialize it with the address of my_point. */

My mistake was using the pointer to point to the structure and not to the structure's address. Pointers can point only to addresses (they store its value), they cannot point to objects.

carloswm85
  • 1,396
  • 13
  • 23