Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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)
{
}
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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)
{
}

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)
 

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
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)
{
}

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

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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

macrumors 6502
Mar 12, 2010
274
0
#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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.