C语言 从不兼容的指针类型警告传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2160421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:18:49  来源:igfitidea点击:

Passing argument from incompatible pointer type warning

cpointers

提问by hora

I've been trying to figure out pointers in C most of today, even asked a questionearlier, but now I'm stuck on something else. I've got the following code:

我今天大部分时间都在试图找出 C 中的指针,甚至早些时候问过一个问题,但现在我被困在别的事情上了。我有以下代码:

typedef struct listnode *Node;
typedef struct listnode {
    void *data;
    Node next;
    Node previous;
} Listnode;

typedef struct listhead *LIST;
typedef struct listhead {
    int size; 
    Node first;
    Node last; 
    Node current; 
} Listhead;

#define MAXLISTS 50

static Listhead headpool[MAXLISTS];
static Listhead *headpoolp = headpool;

#define MAXNODES 1000 

static Listnode nodepool[MAXNODES];
static Listnode *nodepoolp = nodepool;

LIST *ListCreate()
{
    if(headpool + MAXLISTS - headpoolp >= 1)
    {
        headpoolp->size = 0;
        headpoolp->first = NULL;
        headpoolp->last = NULL;
        headpoolp->current = NULL;
        headpoolp++;
        return &headpoolp-1; /* reference to old pointer */

    }else
        return NULL;
}

int ListCount(LIST list)
{
    return list->size;

}

Now in a new file I have:

现在在一个新文件中我有:

#include <stdio.h>
#include "the above file"

main()
{
    /* Make a new LIST */
    LIST *newlist; 
    newlist = ListCreate();
    int i = ListCount(newlist);
    printf("%d\n", i);
}

When I compile, I get the following warning (the printfstatement prints what it should):

当我编译时,我收到以下警告(该printf语句打印出它应该的内容):

file.c:9: warning: passing argument 1 of ‘ListCount' from incompatible pointer type

Should I be worried about this warning? The code seems to do what I want it to, but I'm obviously very confused about pointers in C. After browsing questions on this site, I found that if I make the argument to ListCount (void *) newlist, I don't get the warning, and I don't understand why, nor what (void *)really does...

我应该担心这个警告吗?代码似乎按照我的意愿行事(void *) newlist,但我显然对 C 中的指针感到非常困惑。在本网站上浏览问题后,我发现如果我向 ListCount 提出论点,我不会收到警告,并且我不明白为什么,也不明白什么是(void *)真的......

Any help would be appreciated, thanks.

任何帮助将不胜感激,谢谢。

回答by Alok Singhal

You're getting confused because of multiple typedefs. LISTis a type representing a pointer to struct listhead. So, you want your ListCreatefunction to return a LIST, not a LIST *:

由于多个 typedef,您会感到困惑。 LIST是表示指向 的指针的类型struct listhead。因此,您希望ListCreate函数返回 a LIST,而不是 a LIST *

LIST ListCreate(void)

The above says: ListCreate()function will return a pointer to a new list's head if it can.

上面说:ListCreate()如果可以,函数将返回一个指向新列表头部的指针。

Then you need to change the returnstatement in the function definition from return &headpoolp-1;to return headpoolp-1;. This is because you want to return the last available head pointer, and you have just incremented headpoolp. So now you want to subtract 1 from it and return that.

然后您需要return将函数定义中的语句从return &headpoolp-1;更改为return headpoolp-1;。这是因为您想返回最后一个可用的头指针,而您刚刚增加了headpoolp. 所以现在你想从中减去 1 并返回它。

Finally, your main()needs to be update to reflect the above changes:

最后,您main()需要更新以反映上述更改:

int main(void)
{
    /* Make a new LIST */
    LIST newlist;  /* a pointer */
    newlist = ListCreate();
    int i = ListCount(newlist);
    printf("%d\n", i);
    return 0;
}