0

I have the following declaration:

#define ERROR   0x01
..
UINT32 res=ERROR;

Is it possible, given just the variable res, to copy the string "ERROR" into a char array?

CLARIFICATION:

Maybe I wasn't clear enough. I'm calling a DLL which return values. Those values can be interpreted like this:

switch (res)
{
    case ERROR:
     strcpy(arr, "ERROR");break;
    case ... 
}

since there are many options and the switch will be huge, I wandered maybe there is a shortcut...

3
  • I didn't understand what you want to ask... Please give some example, illustration.
    – 0xF1
    Oct 16, 2013 at 10:00
  • Before compilation, there is a Preprocessor program which accepts your program with preprocessing statements, turns it into a pure form of program without preprocessing statements. Hence, ERROR is replaced with 0x01 wherever in the program, even before compilation starts.. If yours is a C program compiling with gcc: try gcc -E file_name.c to get the pre-processed output.
    – smRaj
    Oct 16, 2013 at 10:11
  • possible duplicate of Is there a simple script to convert C++ enum to string?
    – MSalters
    Oct 16, 2013 at 11:59

3 Answers 3

0
#define ERROR   0x01

defines constant integral literal 0x01 and preprocessor turns the following line:

UINT32 res = ERROR;

into:

UINT32 res = 0x01;

before the compilation starts. There is no char array.


In case you want to copy associated name of constant to avoid doing something like:

case ERROR:
 strcpy(arr, "ERROR"); break;
case X:
 strcpy(arr, "X"); break;
case Y:
 strcpy(arr, "Y"); break;
...

then you might create a helper with static structure retrieving name by given code, yet for the sake of type safety I would avoid #defines as much as possible. Something like (this is just a concept):

const char* getRetCodeName(const UINT32 code) {
    static std::map<int, const char*> codes;
    static int firstCall = 1;
    if (firstCall) {
        codes[ERROR] = "ERROR";
        codes[X] = "X";
        codes[Y] = "Y";
        firstCall = 0;
    }
    return codes[code];
}

and in caller's code:

UINT32 res = someCall();
const char* retCodeName = getRetCodeName(res);
0

It's not possible to do this without depending on the name ERROR, but you can do something like this:

const char* value_to_string(UINT32 val) {
  #define CHECK_CONSTANT(maConst) \
    if (val == maConst) return #maConst

  CHECK_CONSTANT(ERROR);
  return 0;

  #undef CHECK_CONSTANT
}

This abstracts from the value of the macro, but unfortunately it cannot abstract from its name. If you have more than one such #define, you can also list them all as subsequent CHECK_CONSTANT(something); lines.

0

Do you want to display the name of the define? This is not possible. Perhaps you are looking for something like:

myString=(res==ERROR)?"ERROR":"OK"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.