redefine define

Discussion in 'Mac Programming' started by farmerdoug, Feb 11, 2012.

  1. macrumors 6502a

    Sep 16, 2008
    Is there away to do some thing like this?
    if (!strcmp(run, "Hi_Vol_x"))
    	#define CONDITION VOL2[l][i] > vmax[i]
    if (!strcmp(run, "Low_Vol_x"))
    	#define CONDITION VOL2[l][i] < vmin[i]
    if (CONDITION)
  2. macrumors G5


    Nov 25, 2005

    static bool sConditionHiVol;
    static bool sConditionLoVol;
    sConditionHiVol = (strcmp (run, "Hi_Vol_x) == 0);
    sConditionLoVol = (strcmp (run, "Low_Vol_x) == 0);
    #define CONDITION (sConditionHiVol ? (VOL2[l][i] > vmax[i]) :sConditionLoVol ? (VOL2[l][i] < vmin[i]) : false)
  3. thread starter macrumors 6502a

    Sep 16, 2008
    Good morning.
    I'm looking over your solution but
    as usual, I think I left out some important information.
    I want to do this is in a procedure I am calling. In my example, run comes from outside the procedure.

    Which_Vol(char *run)

    Does your example still apply?


    Looks like it worked.

  4. thread starter macrumors 6502a

    Sep 16, 2008

    Having copied, pasted, and gotten the code to work. I went through it to see what I had actually done.


    Thanks again.
  5. macrumors Pentium


    Jan 28, 2009
    Quebec, Canada
    You're mixing compile time definitions and run-time condition checking. You do understand that #define statements are run through the pre-processor before compilation is done on the file right ?

    Something gnasher didn't explain but is quite necessary to understand.
  6. thread starter macrumors 6502a

    Sep 16, 2008
    Right. Which is my original attempts didn't work. But a single define statement
    will work inside main. But I take it that bad form.
  7. macrumors 6502

    Mar 12, 2010
    #define is for the preprocessor, it doesn't know anything about main. You can think of it more as a substitution rule that occurs before the actual C code is compiled. If you had code like:
    # define PI 3.14159
    double pi = PI;

    then when you compiled the code, the preprocessor would replace PI with 3.14159, so the actual code that gets compiled is:
    double pi = 3.14159;

    Your original attempts didn't work because you're confusing runtime behavior with compile time behavior.

Share This Page