Rivf
Rivf

Reputation: 125

Passing a Linked List (incompatible pointer type error)

I am getting the following error when I try to call the function that should initialize my linked list.

 passing argument 1 of 'init_list' from incompatible pointer type [-Wincompatible-pointer-types]
  init_list(list); 

I do not understand why I am getting this error. This is my first time working with linked lists so any help is appreciated. Here is how I set up my linked list along with the prototype for my function:

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

typedef char DATA_TYPE;


typedef struct node_t
{
    DATA_TYPE item;
    struct node_t* next;
} node;

typedef struct
{
    node* head;
    int len;
} linkedlist_t;

void init_list(linkedlist_t* plist);

This is the function:

void init_list(linkedlist_t* plist)
{

    plist->head = (node *)malloc(sizeof(node));
    plist->head->next = NULL;
    plist->len = 0;
}

And this is how I called the function in main.


#include "linkedlist.h"

int main(void) {


    node list;

    init_list(&list);


    return 0;
}

Upvotes: 0

Views: 83

Answers (1)

Devolus
Devolus

Reputation: 22094

You are passing a node where your function expects a linkedlist. These two objects are not compatible, because a node is only a part of a linkedlist.

If your init_list function only initializes the node part, then you should change the signature to expect a node instead and maybe also rename it to init_node which would make the code clearer to understand for a reader.

void init_node(node *plist);

Otherwise change list to be of type linkedlist.

linkedlist_t list;

init_list(&list);

Upvotes: 1

Related Questions