I’m looking to implement a dictionary data structure in C which I want to be as generic as possible. That is to say it can accept a pair of values that can be of any type.
How can I init a variable that can accept any types?
And how can I convert that type back to a type I want? (Typecast)
Thanks.
In C there is no easy way to do this. You can use void* and pass around pointers to the types, but there is no concept of templates or generics or variants that exists.
To use void* you would need to treat everything as a pointer so would have to allocate it on the heap. You would then need to cast those pointers to void* when sending to this data structure and then cast back on the other side. This can be tricky because you have to remember what the types were to start with.
If you happen to be programming on Windows you can use Variants to do this, but there is some overhead associated with it.