Jump to content
  • Advertisement
Sign in to follow this  
LMRFUD

Simple Stupid If Question

This topic is 5089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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) )


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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 this
if(var == NULL)...

// if you leav out the second =, then var will be set to zero
if(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 way
if(NULL == var)...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!