No, there is no way to figure this out from a pointer. This will require that type information be stored in a specific place in all structures at runtime, which is simply not the way C uses the machine.
A common solution for the user of the data type is to provide the print function required by the application, since the application will know the type of data stored. That is, there is usually an iteration function that takes a pointer to a function, calling a user function (which can print an item) for each item in the list.
Here's what the function might look like:
void LinkedList_foreach(const LinkedList *start, bool (*func)(void *element, void *data), void *data);
The above should call func() for each item in the list, passing it the item data and an additional user data pointer, which the caller can use to maintain state for traversal. The func() callback should return false to stop the iteration, true to continue.
To print an integer, assuming the integers are stored in pointers, you can:
static bool print_int(void *element, void *data) { printf("%d\n", (int) element); return true; }
Also, do not leave the return value of malloc() in C.
source share