3

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.

Spencer D
  • 3,376
  • 2
  • 27
  • 43
  • So you mean you need to associate your enum values with a string? If so then it has to be pre-defined isn't it ? I mean along with enum there has to be strings defined ? – Gopi Jan 16 '15 at 17:58
  • @Gopi, yes, that's exactly what I am trying to do. Yes, I would be predefining the string descriptions which should be associated with the error codes. – Spencer D Jan 16 '15 at 18:08
  • 1
    It will be the same work writing a `switch` statement as any other, possibly less. As these are error code descriptions, execution time is probably not an issue, so I would just get on with it. Easy to write and maintain. – Weather Vane Jan 16 '15 at 18:15
  • 2
    Here is an interesting answer. http://stackoverflow.com/questions/9907160/how-to-convert-enum-names-to-string-in-c – Weather Vane Jan 16 '15 at 18:20
  • Actually, that looks promising. Thank you very much, @WeatherVane. I'm not sure if that will be the solution I implement, but that does look useful. – Spencer D Jan 16 '15 at 18:22

1 Answers1

7

Your proposed fallback solution of a function that returns strings for each of the discontinuous error code looks like a good solution to me.

A nice solution to keep enum and messages lined up and to ensure that each enumerated value has its message are X macros. (I now see that Weather Vane has beaten me to this answer by posting a link.)

Here's how that might look applied to your problem:

#include <stdlib.h>
#include <stdio.h>

#define ERROR_CODES(X)                                          \
    X(0,    LIB_SUCCESS,            "No error at all!")         \
    X(10,   LIB_FAIL,               "An error occurred")        \
    X(20,   LIB_NULL_PARAM,         "Illegal null param")       \
    X(30,   LIB_MALLOC_ERROR,       "Allocation failed")        \
    X(40,   LIB_TIMEOUT,            "Connection timed out")     \
    X(-5,   LIB_CONNECTION_CLOSED,  "Connection closed")        \

#define ERROR_ENUM(ID, NAME, TEXT) NAME = ID,
#define ERROR_TEXT(ID, NAME, TEXT) case ID: return TEXT;

enum {
    ERROR_CODES(ERROR_ENUM)
};

const char *error_msg(int code)
{
    switch (code) {
        ERROR_CODES(ERROR_TEXT)
    }

    return "Unknown error";
}

int main()
{
    puts(error_msg(LIB_TIMEOUT));
    puts(error_msg(LIB_CONNECTION_CLOSED));
    puts(error_msg(LIB_SUCCESS));

    return 0;
}
Community
  • 1
  • 1
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • I have actually never heard of X Macros, so I'll have to read about them. This appears to be an excellent and clean solution though! Thank you very much. – Spencer D Jan 16 '15 at 18:32