Although it runs correctly, the following results in the aforementioned compiler warning:
return ((item - (my->items))/(my->itemSize));
‘item’ is a ‘void *’; ‘my->items’ is a ‘void *’; ‘my->itemSize’ is an ‘int’
Casting ‘item’ and ‘my->items’ as an ‘int *’ caused the program to run improperly. What is the best way to remove the warning?
Additions and subtractions with pointers work with the size of the pointed type:
Semantically speaking, the size of
voidshould be zero, since it doesn’t represent anything. For this reason, pointer arithmetic onvoidpointers should be illegal.However, for several reasons,
sizeof(void)returns 1, and arithmetic works as if it was acharpointer. Since it’s semantically incorrect, you do, however, get a warning.To suppress the warning, use
charpointers.