Managing error strings in C

Brian Lambert blambert at lotus.lotus.com
Fri Jan 11 04:22:27 AEST 1991


Hi:

I was wondering if anyone out there had any clever ways of handling
error messages in C.  That is, in small/medimum size programs one
usually winds up with all sorts of:

    PrintLog("Memory allocation error");

lines in the program.  I have seen code where people define an array of
char pointers to error messages used in the program such as:

    char *errors[] = {
        "Memory allocation error",
        "Can't open file",
        ...
    };

    #define NO_MEMORY    0
    #define CANT_OPEN    1

and then prints out the error message by:

    PrintLog(errors[NO_MEMORY]);

I'm sure there are a zillion ways to do this.  I have used this method
in the past, but was never very happy with it.  (It's not very elegant,
and is difficult to maintain as one must be sure to use the proper
number in the #define.)

Got a better idea?

Thanks!

--

Brian Lambert
Lotus Development Corporation
blambert at lotus.com



More information about the Comp.lang.c mailing list