Passing argument from incompatible pointer type warning

前端 未结 1 1317
野性不改
野性不改 2021-01-13 20:04

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:



        
相关标签:
1条回答
  • 2021-01-13 20:22

    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)
    

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

    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.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题