I am learning C and attempting a crude implementation of a linked list in C. Long story short I have a struct containing only a void pointer(element) and another pointer to the next node.(code to follow) My question is, when passing the head node and some other node into a new function, is there any way to ensure the two elements are of the same type? The two nodes should be able to hold any kind of data. Ive tried comparing with sizeof(), but am unable to deference a void pointer. Thanks in advance!
struct Node{
void* element;
struct Node* next;
}
This is the code for the nodes, I just need a way to compare them with assert to ensure a linked list with all of the same element types! Thanks!
No -- you generally want to avoid a design like this, but if you really can't avoid it, you typically need to put a enum in the node to tell you the type of data it contains.
A void* is precisely a type-less pointer. In other words, all that your program knows is that it's a pointer to SOMETHING. This is useful, but it specifically (intentionally) isn't what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With