【发布时间】:2011-02-17 09:10:22
【问题描述】:
考虑这段代码:
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_entry 的行中返回一个警告 initialization from incompatible pointer type。
我在相同代码的其他地方使用了 list_entry,它的工作没有任何故障。所以唯一让我印象深刻的可能是我将意外的变量类型传递给了函数,因此附加了结构 Trade 的定义。即使那时问题仍然存在。
希望知道哪里出了问题。
EDIT :这只是一个大代码的小sn-p。我很抱歉让它看起来像我试图在不存在时使用 Trade* 对象。在代码中,我确实使用了typedef来定义struct Trade;
【问题讨论】:
-
如果不包括 list_entry 的定义,我们怎么知道这里发生了什么?大概它是一个宏,因为您将类型(交易)传递给它。附言我猜它应该是 Trade*,而不是 Trade,但我在没有定义的情况下在黑暗中拍摄。
-
list_entry() 函数定义在 LINUX KERNEL 的 list.h 中。List.h 是内核链表的标准头文件。如果您坚持,我可以附上代码
标签: c linux gcc linux-kernel gnu