Public Group

# Simple Stupid If Question

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

## 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 on other sites
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 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 on other sites
Quote:
 Original post by LMRFUDWhat 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) )

Thank You!

##### 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 on other sites
Quote:
 Original post by nifeWell, 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 on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by nifeWell, 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 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 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)...

##### Share on other sites
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"

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 13
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007279

×