Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C list.h中的list_entry()返回警告_C_Linux_Gcc_Linux Kernel_Gnu - Fatal编程技术网

C list.h中的list_entry()返回警告

C list.h中的list_entry()返回警告,c,linux,gcc,linux-kernel,gnu,C,Linux,Gcc,Linux Kernel,Gnu,考虑这段代码: struct Trade { float Price; char* time; int shares; struct list_head *tradeList; }; typedef struct Trade Trade; void DisplayTrades(Trade* TBook) { if(TB

考虑这段代码:

 struct Trade
        {
         float Price;
             char* time;
             int shares;
             struct list_head *tradeList;
        };
typedef struct Trade Trade;

    void DisplayTrades(Trade* TBook)
        {
            if(TBook==NULL)
            {
                printf("NO TRADES\n");
                return;
            }
            struct list_head *pos;
            Trade *tmp;
            pos = TBook->tradeList;
            __list_for_each(pos,TBook->tradeList)
            {
                tmp = list_entry((pos),Trade,tradeList);
                printf("Price %f, Time %s, Shares %d\n",tmp->Price,tmp->time,tmp->shares);
            }

        }
在这段代码中,当我编译时,编译器gcc返回一条警告,提示在调用list\u条目的行中
初始化来自不兼容的指针类型。
我在同一代码的其他地方使用了list_条目,它的工作没有任何问题。因此,唯一让我震惊的是,我可能向函数传递了意外的变量类型,因此附加了结构交易的定义。即使在那时,问题仍然存在

如果能知道哪里出了问题,我将不胜感激


编辑:这只是一段大代码的小片段。我很抱歉让它看起来像是,我试图在不存在的情况下使用Trade*对象。在代码中,我确实使用了typedef来定义struct Trade

DisplayTrades函数中,将交易结构的指针声明为必须使用的参数


struct Trade*Tbook.

DisplayTrades函数中声明交易结构指针作为其参数时,必须使用


struct Trade*Tbook.

若要执行此操作,pos必须是实际的列表头指针,但结构字段应该是列表头而不是列表头指针:

struct Trade
        {
             float Price;
             char* time;
             int shares;
             struct list_head tradeList;
        };
然后:

  void DisplayTrades(Trade* TBook)
        {
            if(TBook==NULL)
            {
                printf("NO TRADES\n");
                return;
            }
            struct list_head *pos;
            Trade *tmp;
            __list_for_each(pos,&TBook->tradeList)
            {
                tmp = list_entry((pos),Trade,tradeList);
                printf("Price %f, Time %s, Shares %d\n",tmp->Price,tmp->time,tmp->shares);
            }

        }

要使其工作,pos必须是实际的列表头指针,但结构字段应是列表头而不是列表头指针:

struct Trade
        {
             float Price;
             char* time;
             int shares;
             struct list_head tradeList;
        };
然后:

  void DisplayTrades(Trade* TBook)
        {
            if(TBook==NULL)
            {
                printf("NO TRADES\n");
                return;
            }
            struct list_head *pos;
            Trade *tmp;
            __list_for_each(pos,&TBook->tradeList)
            {
                tmp = list_entry((pos),Trade,tradeList);
                printf("Price %f, Time %s, Shares %d\n",tmp->Price,tmp->time,tmp->shares);
            }

        }

@算法专家@Lundin请看一下我的编辑。我忘了将它添加到代码段中,但从中获取它的较大代码库确实包含一个typedef
struct Trade(现在添加到代码段中)或者更好,始终使用“struct trade”。这避免了typedef引起的名称空间污染。@Lundin结构typedef在Linux内核代码中是不受欢迎的。这对于任何想要在上游共享代码的人来说都是一个糟糕的建议。@user611775没有“名称空间污染”这样的事情。专业程序员在他们的编码标准中有变量/函数/类型命名,并使用命名前缀。@Eric除了宗教信仰之外,还有什么特别的原因吗?@Algorithmist@Lundin请查看我的编辑。我忘了将它添加到代码段中,但从中获取它的较大代码库确实包含一个typedef
struct Trade(现在添加到代码段中)或者更好,始终使用“struct trade”。这避免了typedef引起的名称空间污染。@Lundin结构typedef在Linux内核代码中是不受欢迎的。这对于任何想要在上游共享代码的人来说都是一个糟糕的建议。@user611775没有“名称空间污染”这样的事情。专业程序员在他们的编码标准中有变量/函数/类型命名,并使用命名前缀。@Eric除了宗教信仰之外,还有什么特殊原因吗?如果不包括列表项的定义,我们怎么知道这里发生了什么?假设它是一个宏,因为您正在向它传递一个类型(交易)。另外,我猜它应该是Trade*,而不是Trade,但我在黑暗中拍摄,没有定义。list_entry()函数是在LINUX内核的list.h中定义的。list.h是内核链表的标准头文件。我可以附上代码,如果你认为我们怎么知道这里发生了什么,当你不包括列表项的定义?假设它是一个宏,因为您正在向它传递一个类型(交易)。另外,我猜它应该是Trade*,而不是Trade,但我在黑暗中拍摄,没有定义。list_entry()函数是在LINUX内核的list.h中定义的。list.h是内核链表的标准头文件。我可以附上密码,如果你知道这正是我害怕的事情。谢谢,是的,我也有同样的预感。嗯,正是我害怕的事情。谢谢,是的,我也有同样的预感。