0

I am trying to add nodes right after the *add pointer in my linked list my struct and code is as follows, but shows an error in (*add)->next = new_node:

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

    void create_node(node **add,int dat)
    {
        if((*add) == NULL)
        {
            (*add) = (node*)malloc(sizeof(node));
            (*add)->data = dat;
            (*add)->next = NULL;
        }

        else
        {
            node *new_node = (node*)malloc(sizeof(node));
            new_node->data = dat;
            new_node->next = (*add)->next;
            (*add)->next = new_node; //assignment to incompatible pointer type error
        }


    }
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
gPats
  • 315
  • 2
  • 17

1 Answers1

1

You're declaring next as a pointer to struct node, but there's no struct node in your code (only the typedef node).

You need to give a name for the struct in order to refer to it in the declaration of next:

typedef struct node {

Currently struct node refers to a different, unrelated struct (that you haven't defined).

Emil Laine
  • 41,598
  • 9
  • 101
  • 157