gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?)


From: David Ayers
Subject: Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?)
Date: Mon, 02 Feb 2004 12:00:23 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113

Marco Scheurer wrote:

On Feb 2, 2004, at 12:00 PM, Helge Hess wrote:

On Feb 2, 2004, at 6:01 AM, Leigh Smith wrote:

Hm, well, actually I do prefer "if (ptr == NULL)" over "if (!ptr)". Not sure why.


Implicit is the assumption that NULL is always 0, which isn't actually a specification, merely a convention of the compiler that NULL is actually something like:

#define NULL (void *) 0

While unusual for a compiler to declare NULL to be something other than 0, it would be legal C, depending on the processor architecture. I'd definitely favour the more explicit ptr == NULL since you are then articulating that you are checking against a NULL pointer, rather than doing an implicit cast to an integer before then inverting the implicit comparison against 0.


Good point! Just checked the C99 draft:
  http://www.vmunix.com/~gabor/c/draft.html#7.1.6

which indeed proves you right :-) NULL is not required to be defined as 0.


Yes, but it does not matter. Using "if (ptr)" or "if (!ptr)" will still work. The first one is equivalent to "if ((ptr) != 0)" but the compiler will recognize that 0 in this context is a null pointer and use the correct NULL value.

I still prefer  "if (ptr != NULL)", for readability reasons.


I think we are side tracking here a bit. The issue isn't whether we should use "!= NULL" comparison with pointers or similar comparisons with non-BOOL types. I think in general non-BOOL (esp. pointers) should use comparisons to attain a BOOL/truth expression to be used in for if/while/for evaluation. The issue is whether non-YES/NO values for a BOOL are valid.

Cheers,
David




reply via email to

[Prev in Thread] Current Thread [Next in Thread]