| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | NULL |
Re: NULL
By: Torfs Tom to All on Mon Sep 08 2003 07:54 pm
> > I'm not sure we're actually in any sort of disagreement
> > here... If the issue is
> > whether `if (ptr)' is portable, then the answer has to be no
> > since ptr can be
> > any pointer and not necessarily NULL.
>
> Incorrect, this is actually perfectly OK code. In fact I use it all the time
> Here's the relevant question from the comp.lang.c FAQ:
>
> Question 5.3
> Is the abbreviated pointer comparison ``if(p)'' to test for non-null pointer
> valid? What if the internal representation for null pointers is nonzero?
>
> ----------------------------------------------------------------------------
>
> When C requires the Boolean value of an expression (in the if, while, for, a
> 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
> 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 (implici
> 0 is actually a null pointer constant, and use the correct null pointer valu
> 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 matt
>
> 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)
The FAQ is wrong on this question. There is no "internal representation of a
null pointer" as far as the compiler is concerned.
The C Library (and the stddef.h header file) determine the value of NULL. So if
for example, the C Library used -1 for NULL:
if(p)
is *not* equivalent to:
if(p!=NULL)
digital man
Snapple "Real Fact" #132:
A crocodile cannot move its tongue.
--- SBBSecho 2.00-Win32
* Origin: Vertrauen - vert.synchro.net (1:103/705)SEEN-BY: 633/267 270 @PATH: 103/705 218/903 10/3 106/2000 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™.