I am implementing some data structures in C, with the goal of being able to use them easily for future projects. One possible way to do this is to implement each of the data structures in a header file.
For example, here is linked_list.h:
#ifndef LINKED_LIST
#define LINKED_LIST
#include <stdlib.h>
typedef struct linked_list_type {
int val;
struct linked_list_type* next;
} linked_list;
// Initializes a single node with a value v
linked_list* ll__init(int v) {
linked_list* new_ll = malloc(sizeof(linked_list));
new_ll->val = v;
new_ll->next = NULL;
return new_ll;
}
// More functions
#endif
This works nicely, since I can just use #include "linked_list.h"
to get the linked_list struct and all its functions in a future project.
However, it goes against the normal practice of just using declarations (and not implementations) in a header file. So, I have some questions:
- Is there a better way to get easy inclusions like this? From some searching, it seems that including a .c file is a bad idea.
- Is what I'm doing right now bad/dangerous in a way that I don't realize?