user6464839
user6464839

Reputation:

Linked list using typedef

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

typedef struct
{
    int value;
    struct node* next;
} node;

void print(node* list);

int main()
{
    node* n1;
    node* n2;
    n1 = (node *) malloc(sizeof(node));
    n2 = (node *) malloc(sizeof(node));

    n1->value = 4;
    n1->next = n2;
    n2->value = 5;
    n2->next = NULL;

    print(n1);
    return 0;
}

void print(node* list)
{
    node* p;
    p = list;
    while (p != NULL)
    {
        printf("%d ", p->value);
        p = p->next;
    }
}

The code works but my compiler (gcc) gives a warning that n1->next = n2 is (assignment from incompatible pointer type) What does it mean? and how do I avoid it?

Upvotes: 2

Views: 6553

Answers (1)

rici
rici

Reputation: 241921

typedef struct {...} node declares node to be a type alias for a struct without a tag. In C, two otherwise identical structs with different tags are not the same structure. (Furthermore, two declarations of structs without tags are different structs.) The declaration does not declare struct node at all. So struct node* next; refers to a different, incomplete struct. (It's ok that its incomplete, because you only use it as a pointer.)

Here's a a better way of doing it. (Changing node to Node is just my style; I find it easier when type names are easily distinguishable from variable names.) Note the predeclaration of the typedef, which allows its use inside the later struct definition. (It's still incomplete when its used inside the definition, but that's OK because it's a pointer.)

I also changed the malloc calls to use the type of the variable being defined rather than the typename, which is generally better style. (And I removed the unnecessary casts; if you plan to use C++, you should change the malloc to a new rather than adding a C-style cast.)

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

typedef struct Node Node;
struct Node {
    int value;
    Node* next;
};

void print(Node* list);

int main()
{
    Node* n1 = malloc(sizeof *n1);
    Node* n2 = malloc(sizeof *n2);

    n1->value = 4;
    n1->next = n2;
    n2->value = 5;
    n2->next = NULL;

    print(n1);
    return 0;
}

void print(Node* list)
{
    Node* p;
    p = list;
    while (p != NULL)
    {
        printf("%d ", p->value);
        p = p->next;
    }
}

(Live on coliru)

Upvotes: 2

Related Questions