A factory pattern can also be implemented in C, suppose the following operations are defined by your interface:
typedef int (*operations_1) (void *data);
typedef int (*operations_2) (void *data);
typedef struct impl_ops_t
{
operations_1 op1;
operations_2 op2;
} impl_ops_t;
So, in order to be able to get an instance of implementations that implements such an interface, you must define a structure with both data and operations, then you can define a creation that will return this structure and, possibly, destruction operations:
typedef struct ctx_t
{
void *data;
impl_ops_t *operations;
} ctx_t;
typedef ctx_t (*create_handle) (void);
typedef void (*destroy_handle) (ctx_t **ptr);
typedef struct factory_ops
{
create_handle create;
destroy_handle destroy;
} factory_ops;
You should also define a function that provides you with factory methods, perhaps you should have a way to get the right implementation based on your needs (maybe not a simple parameter, as in the example below):
typedef enum IMPL_TYPE
{
IMPL_TYPE_1,
IMPL_TYPE_2
} IMPL_TYPE;
factory_ops* get_factory(int impl_type);
Therefore, it will be used as:
main (...)
{
factory_ops fact = get_factory(IMPL_TYPE_2);
ctx_t *c = fact->create();
int a = c->operations->op1(c->data);
int b = c->operations->op2(c->data);
fact->destroy(&c);
return 0;
}