Reputation: 873
Consider this piece of code:
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);
}
}
In this code, when I compile, the compiler gcc returns a warning that initialization from incompatible pointer type
in the line where list_entry is evoked.
I have used list_entry in other places of the same code, and its working without any glitch. So the only thing that struck me was perhaps I passed unintended variable types to the function, hence attached the definition of structure Trade.The problem persists even then.
Would appreciate,to know where things are going wrong.
EDIT : This is just a small snippet from an otherwise large code. I apologise for making it look like, that I am trying to use Trade* object when none exists. In the code, I have indeed used typedef to define struct Trade;
Upvotes: 0
Views: 1012
Reputation: 15406
For this to work, pos must be an actual list head pointer, but the structure field shoud be a list_head and not a list_head pointer :
struct Trade
{
float Price;
char* time;
int shares;
struct list_head tradeList;
};
And then :
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);
}
}
Upvotes: 3
Reputation: 6695
In DisplayTrades function while declaring the pointer of Trade structure as its argument you must use
struct Trade * Tbook.
Upvotes: 0