I've currently got some problems with the implementation of "data type agnostic", abstract data structures in C -- for example a simple stack. I'd like to make it independent from specific data types to store, and specify the type later when making use of the stack.
I know there is the "void pointer" concept which can be used to point to any data structure/type. But how do I accomplish "void pointer arithmetic" without knowing the stored "target" data structure/type from the stack algorithms' point of view? I'd like to abstract my data structures as much as possible in order to gain a maximum of reusability.
A possible solution that comes to my mind, would be wrapping the data to store, in a struct like the following:
Code: Select all
struct stack_node {
void* data;
struct stack_node* next;
}
Are there any elegant techniques available or am I tied to using my above solution?
Thanks in advance,
Paw