21 Oct, 2011, arholly wrote in the 1st comment:
Votes: 0
OK, so I want to make it so that I do not have to keep typing in "ch->race == race_lookup("vampire")" every time I want to check and see if someone is a vampire. Now, I want to put it in the merc.h file (or our equivalent).

Now, I think I've got it right, but want to make sure. I want it to check if they are a vampire, regardless of if they are a PC or NPC. It should be:
#define IS_VAMPIRE(ch) (ch->race == race_lookup("vampire"))

That's correct right? That would allow me to simplify "ch->race == race_lookup("vampire")" to IS_VAMPIRE correct?

Arholly
21 Oct, 2011, arholly wrote in the 2nd comment:
Votes: 0
Nevermind, yes I am.
22 Oct, 2011, kiasyn wrote in the 3rd comment:
Votes: 0
IIRC correctly it should look like:
#define IS_VAMPIRE(ch) ((ch)->race == race_lookup("vampire"))


without putting ch in () it will use the literal ch, as in it will only work if you have a ch variable defined in the local scope. surrounding it with () means that you can do IS_VAMPIRE(victim) and it will still work.

but it has been a long time since i used c so could be all wrong :P
22 Oct, 2011, Twisol wrote in the 4th comment:
Votes: 0
kiasyn said:
but it has been a long time since i used c so could be all wrong :P

Wrong, I believe. Wrapping it in parentheses just ensures that the syntax of what you pass to the macro doesn't interfere with the syntax of the macro itself.
22 Oct, 2011, David Haley wrote in the 5th comment:
Votes: 0
kiasyn said:
without putting ch in () it will use the literal ch, as in it will only work if you have a ch variable defined in the local scope. surrounding it with () means that you can do IS_VAMPIRE(victim) and it will still work.

Yeah… no, that's completely wrong. :wink: It has nothing at all to do with local scope etc.

It has to do with the fact that a macro isn't a function, and so the substitution occurs literally.

consider:

#define SQUARE(x) (x * x)

now:

SQUARE(1+2)

expands to

(1+2 * 1+2)

which yields 5 rather than the expected result 9.

The safe parenthesization of the parameters,

#define SQUARE(x) ((x) * (x))

makes sure that the expression is evaluated as expected.

Of course, if x happens to be a function call, that function call will still be made twice, again unlike if SQUARE were a function. But that's a problem for another day.

People tend to think of macros as functions, which they really aren't at all; they're just a syntactic transformation.
22 Oct, 2011, Runter wrote in the 6th comment:
Votes: 0
I avoid macros in C whenever reasonable to do so.
22 Oct, 2011, kiasyn wrote in the 7th comment:
Votes: 0
David Haley said:
kiasyn said:
without putting ch in () it will use the literal ch, as in it will only work if you have a ch variable defined in the local scope. surrounding it with () means that you can do IS_VAMPIRE(victim) and it will still work.

Yeah… no, that's completely wrong. :wink: It has nothing at all to do with local scope etc.


i had a sneaky suspicion that i was talking complete bullshit :P
23 Oct, 2011, Exodus wrote in the 8th comment:
Votes: 0
Runter said:
I avoid macros in C whenever reasonable to do so.

True, macros don't typically get added to the symbol table so debugging can be difficult. Also, in C++ preprocessor macros don't have a scope/namespace.

You can achieve this using a function just the same:
bool is_vampire( CHAR_DATA *ch )
{
if( !ch || !ch->race )
return FALSE;

if( ch->race == race_lookup("vampire") )
return TRUE;
return FALSE;
}
23 Oct, 2011, Runter wrote in the 9th comment:
Votes: 0
bool is_vampire( CHAR_DATA *ch ) {
assert(ch && ch->race);
return ch->race == race_lookup("vampire");
}
0.0/9