I want to hide the nature of a public struct for an API inside my .h/.c couple, so I declare only the typedef in the .h and I complete the declaration in the .c like this:
foo.h
typedef struct filters_s filters_t;
/* some public functions declaration using filters_t */
(...)
foo.c
typedef struct filters_s filter_node_t;
struct filters_s
{
filter_node_t *children[96];
(...)
}
As you can see, filters_s is in fact the root node of a tree so internally, I’m using filter_node_t but externally, I don’t want to expose the “tree” nature of the struct.
So, my “problem” is that ideally I’d like to have also another name for the struct like filter_node_s, but I don’t know if it’s possible.
If you want to hide the implementation of the structure then you need a opaque pointer to the structure. There you would pass this pointer to a function that will get or modify the data of the structure.
The declaration will be in the *.h header file. And the definition will be in the *.c file.
Something like this in the *.h (header file):
Then in the *.c (implementation file):
Then in your *.c (driver file)
I have just typed this in, so it might not be perfect as I haven’t checked for errors. Always remember to free memory after you finish with it.
Hope this helps