Passing an argument from an incompatible pointer type warning

I tried to define pointers to C today, even asked a question earlier, but now I'm stuck on something else. I have the following code:

typedef struct listnode *Node; typedef struct listnode { void *data; Node next; Node previous; } Listnode; typedef struct listhead *LIST; typedef struct listhead { int size; Node first; Node last; Node current; } Listhead; #define MAXLISTS 50 static Listhead headpool[MAXLISTS]; static Listhead *headpoolp = headpool; #define MAXNODES 1000 static Listnode nodepool[MAXNODES]; static Listnode *nodepoolp = nodepool; LIST *ListCreate() { if(headpool + MAXLISTS - headpoolp >= 1) { headpoolp->size = 0; headpoolp->first = NULL; headpoolp->last = NULL; headpoolp->current = NULL; headpoolp++; return &headpoolp-1; /* reference to old pointer */ }else return NULL; } int ListCount(LIST list) { return list->size; } 

Now in the new file I have:

 #include <stdio.h> #include "the above file" main() { /* Make a new LIST */ LIST *newlist; newlist = ListCreate(); int i = ListCount(newlist); printf("%d\n", i); } 

When compiling, I get the following warning (the printf statement prints what it should):

 file.c:9: warning: passing argument 1 of 'ListCount' from incompatible pointer type 

Should I worry about this warning? It seems that the code does what I want, but I obviously got very confused about the pointers to C. After looking at the questions on this site, I found that if I make the ListCount (void *) newlist , I won't get a warning, and I don’t understand why, and what (void *) really does ...

Any help would be appreciated, thanks.

+4
source share
1 answer

You are confused due to multiple typedef. LIST is a type representing a pointer to a struct listhead . So you want your ListCreate function ListCreate return LIST , not LIST * :

 LIST ListCreate(void) 

Above said: ListCreate() function will return a pointer to a new list head, if possible.

Then you need to change the return in the function definition of return &headpoolp-1; before return headpoolp-1; . This is because you want to return the last available pointer to the head, and you just increased headpoolp . So now you want to subtract 1 from it and return it.

Finally, your main() needs to be updated 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; } 
+5
source

Source: https://habr.com/ru/post/1299716/


All Articles