Besides GLib, as others have suggested, you can create your own generic types without any type safety, like a linked list that uses void * as a generic pointer to the next stack object. Some of them even used macros to create template behavior in ANSI C with some reasonable success. Find an example or two in the cplusplus.com forums (there arenβt so many, if I remember correctly).
Alternatively, you could implement your own OOP scheme, as described in Object Oriented Programming with ANSI C , although I would not recommend this. You could also spend the time spent creating this beast (and maybe not understand how it works in the end) and study some of GLib to familiarize yourself with it.
Personally, I prefer the first approach. You lose type safety and get a lot of castings, but this is the fastest way to implement. The real problem with this method is the data that is being stored. std::stack<int> , converted to C, is just a structure stack that has an int data field. What about std::vector<std::stack<int> > , an address list of stacks of integers? C standard cannot guarantee that casting from pointer type to integral type will work without any loss of information. In other words, although you can use pointers, you cannot use prime integers to store information. Perhaps, several expanded specialized templates will work - one for int (one for long?), One for double and one for pointer types.
In the end, the best solution is to use a library such as GLib, but if you prefer to roll on your own, take care to create one. You never know when you will need a combination of item types in a container.
source share