PDA

View Full Version : redefine define




farmerdoug
Feb 11, 2012, 07:40 AM
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)
{
}



gnasher729
Feb 11, 2012, 07:53 AM
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)
{
}


No.

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)

farmerdoug
Feb 11, 2012, 08:08 AM
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.

farmerdoug
Feb 11, 2012, 11:25 AM
gnasher.

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

Neat.

Thanks again.

KnightWRX
Feb 11, 2012, 11:31 AM
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)
{
}


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.

farmerdoug
Feb 11, 2012, 11:43 AM
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.

willieva
Feb 11, 2012, 12:12 PM
#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.