Article View: comp.lang.c.moderated
Article #2393Re: references wanted
From: Tanmoy Bhattacharya
Date: Wed, 27 Mar 1996 00:59
Date: Wed, 27 Mar 1996 00:59
60 lines
2939 bytes
2939 bytes
In article <4imak1$e0j@solutions.solon.com>
ANH@KUHUB.CC.UKANS.EDU writes:
<snip>
A: > printf( "%p\n", (void*) NULL); /* OK */
A: > printf( "%p\n", (Type*) NULL); /* not OK */
A: > printf( "%p\n", NULL); /* not OK */
<snip>
A: Really? Seems like I need to update my 'beliefs' :-) Actually, (TYPE*)0
A: has been recommended to me by several professors of mine who worked as
A: software engineer as places such as Bell Labs.
I think that the basic problem is that there is actually no
alternative to learning a language: such rules of thumb are often
misleading. %p needs a pointer of type void*, and unless Type is same
as void, (Type*)NULL is not such a pointer. In a prototyped function
which specifies void* as a parameter type, (Type*)NULL will be
converted to void*. printf is not such a function. So, passing
(Type*)NULL to printf is plain wrong (when the format specifies %p
that is).
Note, however, that this has nothing to do with NULL, (void*)NULL and
(Type*)NULL have different types, and one may need one or the
other. The usual mistake is, in fact,
Type x;
printf( "%p\n", &x); /* wrong */
printf( "%p\n", (void*) &x); /* OK */
Plain NULL is wrong because of a similar reason. In fact, NULL is not
guaranteed to have a pointer type at all. The only useful thing that
is guaranteed is that when cast, compared, passed as a visibily
prototyped parameter or assigned to a pointer (let's not get technical
with the actual wording here: it is not comp.std.c), NULL becomes a
null pointer of appropriate type. None of these applies in the current
situation, and so use of unadorned NULL is incorrect.
A:
A: Just to re-iterate,
A:
A: tMyType *ptr;
A: if (ptr==NULL) {
A: ...
A: }
A:
A: is a perfectly safe construct?
Yes, see the last para.
A:
A: [Yes, and in fact, so is
A: if (ptr == 0)
A: and it is guaranteed equivalent. -mod]
A:
A: Anh
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID:
<4ja3t2$pca@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4i6fn8$p0g@solutions.solon.com> <4i97jv$8hn@solutions.solon.com> <4ietaq$b0n@solutions.solon.com> <4imak1$e0j@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya) Newsgroups: comp.lang.c.moderated Subject: Re: references wanted Date: 26 Mar 1996 18:59:14 -0600 Organization: Los Alamos National Laboratory Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4ja3t2$pca@solutions.solon.com> References: <4i6fn8$p0g@solutions.solon.com> <4i97jv$8hn@solutions.solon.com> <4ietaq$b0n@solutions.solon.com> <4imak1$e0j@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com