最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

generics - How can I use custom datatypes in my personal C libraries - Stack Overflow

programmeradmin3浏览0评论

I have implemented a linked list library in C. It has all the basic functions to quickly implement a linked list. I stored all that in a header file and an implementation file.

Now I want my linked list to be able to store more information than the standard datatypes like int, char, etc.

So I build everything around a custom datatype_t.

To give an example, this is part of the header file.

typedef int datatype_t;

typedef struct node_s
{
    datatype_t key;
    struct node_s *next_node_p;
    struct node_s *previous_node_p;
}node_t;

//add an element to the rear of the list
int         list_element_add(datatype_t new_key, list_t *list);

Now I have the problem that I want the custom datatype to be a datatype that is defined outside of the library. For example a struct. I think that is the usual case for a linked list. What options do I have to use the functions of my library.

I have implemented a linked list library in C. It has all the basic functions to quickly implement a linked list. I stored all that in a header file and an implementation file.

Now I want my linked list to be able to store more information than the standard datatypes like int, char, etc.

So I build everything around a custom datatype_t.

To give an example, this is part of the header file.

typedef int datatype_t;

typedef struct node_s
{
    datatype_t key;
    struct node_s *next_node_p;
    struct node_s *previous_node_p;
}node_t;

//add an element to the rear of the list
int         list_element_add(datatype_t new_key, list_t *list);

Now I have the problem that I want the custom datatype to be a datatype that is defined outside of the library. For example a struct. I think that is the usual case for a linked list. What options do I have to use the functions of my library.

Share Improve this question edited Mar 31 at 12:48 Weijun Zhou 5,1662 gold badges21 silver badges41 bronze badges asked Mar 31 at 8:24 Peter KirschPeter Kirsch 695 bronze badges 4
  • 2 Store pointers to the data, and the size of the data. So basically you have a void *key; and size_t key_size; – Some programmer dude Commented Mar 31 at 8:31
  • It's also possible to generate the code based on a certain type - similar to templates in C++. However, that's not recommended practice. Neither is using linked lists in the first place. For beginner learning purposes just change the typedef. – Lundin Commented Mar 31 at 9:10
  • You could use the "embedded node control structure" style of linked-list functionality, as used by the Linux kernel, Windows kernel, or the BSD-ish <sys/queue.h> macros. These also allow the same item to be queued on multiple lists. They work by subtracting an offset from the pointer to the list node to obtain a pointer to the containing object. – Ian Abbott Commented Mar 31 at 9:44
  • @Peter Kirsch, "What options do I have to use the functions" --> What do you want to use member struct node_s *next_node_p; for? – chux Commented Mar 31 at 15:44
Add a comment  | 

1 Answer 1

Reset to default 2

Replace datatype_t with in void* library. This allows you to store pointers to any data type (e.g structs, ints, chars, etc.)

typedef struct node_s
{
    void* key;
    struct node_s *next_node_p;
    struct node_s *previous_node_p;
} node_t;

int list_element_add(void* new_key, list_t *list);

since you are making the program more generic so you may face runtime error obviously, and the one who is using the library must manage memory manually using malloc/free

发布评论

评论列表(0)

  1. 暂无评论