Can anyone point me to a reference on how to implement the factory pattern using ANSI C? If more patterns are covered to that would just be a bonus. Doing this in C++ i trivial for me, but since C does not have classes and polymorphism I'm not quite sure how to do it. I was thinking about having a "base" struct with all the common data types and then using void pointers, and defining all the common parts of the structs in the same order as in the base struct at the top? Or is it not guaranteed that they end up in the same way in memory?
C have function pointers and structs. So you can implement classes in C.
something like this should give you a clue.
void class1_foo() {}
void class2_foo() {}
struct polyclass
{
void (*foo)();
};
polyclass make_class1() { polyclass result; result.foo = class1_foo; return result; }