TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Kurt Kuzba
from: Tom Torfs
date: 1998-08-21 23:34:58
subject: NULL pointers

Kurt Kuzba wrote in a message to Robin Sheppard:

RS>   Is testing for a 'false pointer', ie, using the logical
RS>   NOT operator like you did above ANSI standard?

 KK>    No. It isn't. The NULL macro is standard, but the actual
 KK>    value of a NULL pointer is implementation defined.
 KK>    NULL and 0 are interchangeable in most C++ implementations,   
 KK> but I shouldn't have used it thusly in C. Force of habit. 

 But it is!

Extract from the comp.lang.c FAQ:

5.3:   Is the abbreviated pointer comparison "if(p)" to test for non-
   null pointers valid?  What if the internal representation for
   null pointers is nonzero?

A:   When C requires the Boolean value of an expression (in the if,
   while, for, and do statements, and with the &&, ||, !, and ?:
   operators), a false value is inferred when the expression
   compares equal to zero, and a true value otherwise.  That is,
   whenever one writes

      if(expr)

   where "expr" is any expression at all, the compiler essentially
   acts as if it had been written as

      if((expr) != 0)

   Substituting the trivial pointer expression "p" for "expr," we
   have

      if(p)   is equivalent to      if(p != 0)

   and this is a comparison context, so the compiler can tell that
   the (implicit) 0 is actually a null pointer constant, and use
   the correct null pointer value.  There is no trickery involved
   here; compilers do work this way, and generate identical code
   for both constructs.  The internal representation of a null
   pointer does *not* matter.

   The boolean negation operator, !, can be described as follows:

      !expr   is essentially equivalent to   (expr)?0:1
         or to            ((expr) == 0)

   which leads to the conclusion that

      if(!p)   is equivalent to      if(p == 0)

   "Abbreviations" such as if(p), though perfectly legal, are
   considered by some to be bad style (and by others to be good
   style; see question 17.10).

   See also question 9.2.

   References: K&R2 Sec. A7.4.7 p. 204; ANSI Sec. 3.3.3.3,
   Sec. 3.3.9, Sec. 3.3.13, Sec. 3.3.14, Sec. 3.3.15, Sec. 3.6.4.1,
   Sec. 3.6.5; ISO Sec. 6.3.3.3, Sec. 6.3.9, Sec. 6.3.13,
   Sec. 6.3.14, Sec. 6.3.15, Sec. 6.6.4.1, Sec. 6.6.5; H&S
   Sec. 5.3.2 p. 122.

greetings,
Tom
tomtorfs{at}village.uunet.be

--- timEd/2 1.10+
* Origin: 80X86 BBS 32-15-24.62.32 V.34/V.FC (24h/24h) (2:292/516)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 292/516 252/356 140/1 270/101 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.