开发者

list_entry() in list.h returning a warning

开发者 https://www.devze.com 2023-02-12 03:12 出处:网络
Consider this piece of code: struct Trade { float Price; char* time; int shares; struct list_head *tradeList;

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 sa开发者_如何转开发me 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;


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

        }


In DisplayTrades function while declaring the pointer of Trade structure as its argument you must use

struct Trade * Tbook.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号