C structure multiple types -


i'd write library in c , don't know recommended way. got example structure , multiple functions this:

typedef struct example {    int *val;    struct example *next;  } example; 

and have build function multiple types of val

example* build() { sth }; example* buildf() { sth }; // val float example* buildd() { sth }; // val double 

what better practice (used in "professional" library). use pointer void , casting or have structure possibilities - int, float, double.

use union , way store type info:

typedef struct example {    enum{ t_struct_with_int, t_struct_with_float, t_so_on } type;    union {       int val_int;       float val_float;    } val;    struct example *next;  } example; 

access fields after checking type s->val.val_int

in c11 can have union anonymous , fields can accessed s->val_int


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -