从兼容的指针类型的警告传递参数 [英] Passing argument from incompatible pointer type warning

查看:144
本文介绍了从兼容的指针类型的警告传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出用C今天大部分的三分球,连问了<一个href=\"http://stackoverflow.com/questions/2159498/need-some-help-understanding-pointers-and-memory-in-c\">question较早,但现在我卡在别的东西。我有以下code:

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

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);
}

当我编译,我得到以下警告(即的printf 语句打印它应该):

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

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

我应该担心这个警告?在code似乎做什么,我是想,但我显然有关C.指针在这个网站浏览问题之后很困惑,我发现,如果我做参数ListCount (无效*)newlist ,我没有得到警告,我不明白为什么,也没有什么(无效*)确实。 ..

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...

任何帮助将是AP preciated,谢谢。

Any help would be appreciated, thanks.

推荐答案

您是因为多重的typedef的越来越糊涂。 列表是一种可再presenting一个指向结构从ListHead 。所以,你希望你的 ListCreate 函数返回一个列表,而不是 LIST *

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

LIST ListCreate(void)

上面说: ListCreate()功能,如果它可以返回一个指针到一个新的列表的头

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

然后,你需要改变从收益语句在函数定义收益和放大器; headpoolp-1; 收益headpoolp-1; 。这是因为要返回最后一个可用的头指针,你刚刚增加 headpoolp 。所以,现在要减去1从中并返回。

Then you need to change the return statement 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.

最后,你的的main()需要被更新以反映上述变化:

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

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

这篇关于从兼容的指针类型的警告传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆