In a project I am working on, I have defined a basic enum
to store a list of the possible errors the application could encounter. However, I would like to find a way to return a string describing the error.
Here's the enumeration I'm using:
enum _library_results_enum{
LIB_SUCCESS = 1,
LIB_FAIL,
LIB_NULL_PARAM,
LIB_MALLOC_ERROR,
LIB_TIMEOUT,
LIB_CONNECTION_CLOSED
}
If a function returns a result other than LIB_SUCCESS
, then ideally I would like to be able to just say:
printf("Error Description: %s\n", ERROR_DESCRIPTIONS[RESULT]);
To handle that I would imagine I need a static const *char[]
to house an array of all descriptions. However, the enum
values do not start at zero (0) and eventually I might add some negative values to this enum. Thus, using an array of strings isn't really an option. What else could I do to handle this?
I have considered just creating a function which uses a switch statement to return back a description. However, this is really just a fallback solution if there is not a better option.
Edit: To better clarify, what I need is a way to associate result codes within an enumeration to a string describing them.