Take the 2-minute tour ×

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!

share|improve this question
Really I don't see the harm in allowing different types in the nodes. I don't really see the use for it, but I would just trust the calling code to not do something odd like that unless the person writing it truly wanted to. –  Corbin Mar 20 '12 at 4:52
You could do if(a->element == b->element) { /* same type */ } else { /* maybe not the same type */ } :P –  Chris Lutz Mar 20 '12 at 4:57
If it's a pointer to the same place in memory? That seems very pointless. –  Corbin Mar 20 '12 at 4:59
@Corbin - I didn't say it'd be helpful. (Also, I don't know if it was intentional but your "pointless" pun is fantastic.) –  Chris Lutz Mar 20 '12 at 5:03
@ChrisLutz True that you didn't state that it was helpful. I was about to say that comments are assumed to be intended to be helpful, but then I remembered my first comment. It wasn't particularly helpful by a strict standard (though I believe he should just allow a non-homogeneous list and not depend on a hideously-hacky enum approach). And no, it was not intentional :-( –  Corbin Mar 20 '12 at 5:05
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
Can you please tell more about having an enum inside struct for the data type? –  vidit Mar 20 '12 at 4:51
Sure -- you basically just do something like enum types { T_CHAR, T_SHORT, T_INT, T_LONG, T_FLOAT, T_DOUBLE }; (with names for whatever other types you care about) and set it the value corresponding to the type you're storing. The big problem is with extending it (you have to edit the enum every time you add another type to store). Along with that you (typically) need a switch statement switch(type) { case T_CHAR: use_char((char)x.value); case T_SHORT: use_short((short)x.value); /* ... */} –  Jerry Coffin Mar 20 '12 at 4:54
A better way might be to use something like typedef struct { enum type type; int i; } integer; for each type you'll need (where enum type defines a distinct value for each struct) and use these types instead of the raw integers - but then you're technically not using a void * but an enum type * and you might as well make it explicit. If you opt for this approach, a few macros (or x-macros) can cut out some repetition if you live in a country where macros are legal. –  Chris Lutz Mar 20 '12 at 4:55
@JerryCoffin- Thanks :) –  vidit Mar 20 '12 at 4:56
@ChrisLutz: Adding new values isn't the hard part -- adding new code to handle those values is. It's possible to make that extensible too, but rather less trivial. –  Jerry Coffin Mar 20 '12 at 13:16
show 1 more comment

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.

share|improve this answer
add comment

Your Answer


