Attempting more generic c code, custom data structure, in double linked list, this possible? -


i'm attempting refactor code, reduce duplication.

this curated example, defined in each .c file.

struct hrentry_t {     int custom1;     int custom2;     int custom3;      struct hrentry_t *prev, *next; };  struct hrentry_t* attachentry(struct hrentry_t* hentry) {     struct hrentry_t* hnew = calloc(sizeof(struct hrentry_t), 1);     if (hnew == null)         return null;      if (hentry != null) {         while (hentry->next != null) {             hentry = hentry->next;         };         hentry->next = hnew;         hnew->prev = hentry;     }     return hnew; } 

https://github.com/techzilla/check_snmp_extras, entire codebase.

i'm declaring , initializing custom double linked list, , corresponding allocate function. if moved linked list functions , code common lib .c , .h, how can file specific data inside of each list entry? each file requires different types , number of variables.

maybe make double linked list contain prev next , data? somehow make data handle incomplete struct? how need allocated though? i'm open reconsidering approach, solid advice experienced coders appreciated.

one approach make specialized list data types castable generic double-linked list structure. can accomplished putting non-specialized data members @ beginning of structure:

struct node_t {     struct node_t * prev, * next; };  struct hrentry_t {     struct node_t node;      int custom1;     int custom2;     int custom3; }; 

it makes sense cast hentry_t* node_t*. signature of attachment function becomes:

struct node_t* attachentry(struct node_t* node); 

and use it, cast instances of specialized type generic type:

struct hentry_t * my_hentry_ptr;  /* initialized somehow... */ my_list = attachentry((struct node_t*)my_hentry_ptr); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -