简体   繁体   English

传递链接列表(不兼容的指针类型错误)

[英]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.当我尝试调用应该初始化我的链表的 function 时,出现以下错误。

 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:下面是我如何设置我的链表以及我的 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:这是 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 .这就是我在main中调用 function 的方式。


#include "linkedlist.h"

int main(void) {


    node list;

    init_list(&list);


    return 0;
}

You are passing a node where your function expects a linkedlist .您正在传递一个node ,您的 function 需要一个linkedlist These two objects are not compatible, because a node is only a part of a linkedlist .这两个对象不兼容,因为node只是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.如果您的init_list function 仅初始化node部分,那么您应该将签名更改为期望一个node ,并且可能还将其重命名为init_node ,这将使代码更易于读者理解。

void init_node(node *plist);

Otherwise change list to be of type linkedlist .否则将 list 更改为linkedlist类型。

linkedlist_t list;

init_list(&list);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM