Simple Stupid If Question

Started by
8 comments, last by GameDev.net 19 years, 3 months ago
What is the default condition in this IF? LPDIRECTDRAWSURFACE7 extractor = NULL; if (extractor) { extractor->Release(); extractor = NULL; } Is it if TRUE or not NULL? Fud
Advertisement
If you'd actually looked at how NULL was defined, you'd see it was 0.

That means that if statement is:
if (extractor != NULL)


If extractor was NULL, then it'd be NULL != NULL, which is false, but if it's not NULL, then it'd be <some memory address> != NULL, which would hold true.
Erm.....

Well I'm not sure if I'm more confused wit that answer.

I guess all I wanted to know was the condition.

if "if (extractor)" is the same as "if (extractor !=NULL)" just written different?

Thanks
Fud
Quote:Original post by LMRFUD
What is the default condition in this IF?

LPDIRECTDRAWSURFACE7 extractor = NULL;




if (extractor)
{
extractor->Release();
extractor = NULL;
}

Is it if TRUE or not NULL?

Fud


In your case variable, 'extractor' is NULL or 0 so that would mean your if statement evaluates to 'false'. Your if statement could be written as :

if( extractor )if( extractor != 0 )if( !(extractor == 0) )if( extractor != NULL )if( !(extractor == NULL) )
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
Thank You!
Well, I guess you've already figured this out, but true means a positive number, ie. above 0. Everything that is 0 or below is counted as false.
Killers don't end up in jailThey end up on a high-score!
Quote:Original post by nife
Well, I guess you've already figured this out, but true means a positive number, ie. above 0. Everything that is 0 or below is counted as false.


Wrong! False is always zero. True is anything but zero.
Quote:Original post by Anonymous Poster
Quote:Original post by nife
Well, I guess you've already figured this out, but true means a positive number, ie. above 0. Everything that is 0 or below is counted as false.


Wrong! False is always zero. True is anything but zero.


TRUE STORY

(that means he[AP] is correct)
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
Just a suggestion, but never type (var == number) since leaving out the second equal will cause var to be assigned the value in number.

// don't do thisif(var == NULL)...// if you leav out the second =, then var will be set to zeroif(var = NULL)...// oops, var is equal to null now!


Instead, always write (number == var). That way, leaving out the second = will cause a compile time error, not a run time error

// do it this wayif(NULL == var)...
If you're going to offer advice you should probably give a better explanation why. You didn't mention constants at all!

Constants cannot be on the left hand side of an assignment, but they can be on the left hand side of a comparison. So if you always put the constant to the left of a comparision and you accidently forget the second equal sign you will get an error when you compile saying you can't assign something to a constant. You will look at the code and think, "hey! there should be another equals sign"

This topic is closed to new replies.

Advertisement