1

I am trying to store a Preprocessor Constant's value then 'overwrite' it.

My Problem: The code below attempts to store a preprocessor constant's value in variable 'A' then the code undefines that variable then redefines it so it has a new value. The problem is that variable 'A' has the newly defined value and not the old one, if that makes sense. Can I store a Preprocessor Constants value and not its reference(Which is what seems like is happening)?

#ifdef CUSTOM_EVENT_CALLBACK_DEFINED
  #define SUB_CUSTOM_EVENT_CALLBACK_DEFINED CUSTOM_EVENT_CALLBACK_DEFINED
  #undef CUSTOM_EVENT_CALLBACK_DEFINED
#endif
#define CUSTOM_EVENT_CALLBACK_DEFINED "def"

int main()
{
    printf(CUSTOM_EVENT_CALLBACK_DEFINED);     // prints out "def". 
    printf("\n");
    printf(SUB_CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def". I was hoping this would be "abc"
    printf("\n");

    system("PAUSE");
    return 0;
}

// Usage: where the function def is a callback function for a window in a civil engineering program that runs ontop of windows
int def(int id, int cmd, int type)
{
    #ifdef SUB_CUSTOM_EVENT_CALLBACK_DEFINED
      SUB_CUSTOM_EVENT_CALLBACK_DEFINED(id, cmd, type);
    #endif

    // perform my custom code here
}
1
  • macroses in C, C++ are used by preprocessor which acts like text editor, it generates text which later parsed by compiler. Therefore macroses can`t be used in run time which you expect from them. I think the verb "to store" is not applicable to macroses, because no memory in run time && compile time associated with them. Commented Nov 20, 2012 at 6:52

1 Answer 1

2

Short answer - no, it's not possible. Macros don't work like that.

But I doubt you really need to do this. A workaround, for example, would be to store the value in a variable before you overwrite it:

#ifdef CUSTOM_EVENT_CALLBACK_DEFINED
  std::string SUB_CUSTOM_EVENT_CALLBACK_DEFINED = CUSTOM_EVENT_CALLBACK_DEFINED;
  #undef CUSTOM_EVENT_CALLBACK_DEFINED
#else
  std::string SUB_CUSTOM_EVENT_CALLBACK_DEFINED = "";
#endif

#define CUSTOM_EVENT_CALLBACK_DEFINED "def"

int main()
{
    printf(CUSTOM_EVENT_CALLBACK_DEFINED);     // prints out "def". 
    printf("\n");
    printf(SUB_CUSTOM_EVENT_CALLBACK_DEFINED); // prints out "def". I was hoping this would be "abc"
    printf("\n");

    system("PAUSE");
    return 0;
}

Or not use macros at all for this purpose.

Sign up to request clarification or add additional context in comments.

3 Comments

I cant use a string because I am actually coding in .4dm not pure C which is similar but doesn't allow usage of variables outside main or functions - except for Preprocessor defines
is there a way I can copy/store a define by value not reference? Maybe a bubble sort type approach with a temporary variable may be my only option.
@JakeM no. Just think of how it works - #define x y everywhere the preprocessor sees x after this, it will replace it with y - not with what y is defined as.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.