redefine define

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

  1. macrumors 6502a

    #1
    Is there away to do some thing like this?
    Code:
    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

    gnasher729

    #2
    No.

    Code:
    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. macrumors 6502a

    #3
    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.

    thanks.
     
  4. macrumors 6502a

    #4
    gnasher.

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

    Neat.

    Thanks again.
     
  5. macrumors Pentium

    KnightWRX

    #5
    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. macrumors 6502a

    #6
    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

    #7
    #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