I am implementing a simple static library on C, and I have some basic questions regarding the implementation of error handling.
Lets say for the sake of the example, that my library supports a basic linked list which uses Node structure:
typedef struct Node
{
struct Node* next;
struct Node* prev;
int* data;
}Node;
typedef struct LinkedList
{
Node* head;
Node* tail;
}LinkedList;
And let's say, as a part of the interface that I'm providing - I'm giving an implementation for a function that inserts a new Node as the LinkedList head
void insertHead(LinkedList* LinkedList, Node* node)
{
if(LinkedList == NULL || node == NULL)
{
// How to implement?
}
}
I want to catch the most basic error for example - the user of my library tries to enter a NULL pointer. I know that using stderr or printing errors is not a good idea, and also - using asserts is not a good idea.
I somewhat understand that I need to build my own "error - catching mechanism" using a global variable that holds error codes, and converts into strings (Like enums maybe?).
However - I have no idea how to implement such mechanism, how would the user of my library encounter the error itself? the interface itself (the .lib file has only method declerations and such, and the .c file has the implementation) - so after linkage and compilation and such, how would the user get to "see" the errors that I'm providing?
I would love to get a brief example on how to handle user input errors, sorry for being a bit unclear - I am just confused.