use of if (!cptr) and if (cptr), where cptr is a *
Erik Murrey
erik at mpx2.mpx.com
Mon Jul 17 22:29:41 AEST 1989
I'm curious how the gods feel about testing pointers in the following
way (by example):
char *cptr, *malloc(); /* ok, so maybe malloc is void * by now... */
x()
{
cptr= malloc(...);
if (!cptr)
fatal("cannot malloc");
...
}
I know that sizeof(char *) != sizeof(int) on some machines, but the test
of (!cptr) is for zero. It sure looks a lot better when you are testing
complex pointers:
struct xyzzy *sptr;
char *malloc();
x()
{
sptr= (struct xyzzy *)malloc(...);
if (sptr == (struct xyzzy *)NULL) /* (!sptr) would look better */
fatal("cannot malloc");
}
Would (!sptr) break anywhere? Should it be discouraged?
In fact, I much prefer (from a C style article posted a few years back):
x()
{
if (sptr= (struct xyzzy *)malloc(...), !sptr)
fatal("cannot malloc");
}
Any problems with this?
Thanks for your input. E-mail if you feel it is appropriate.
... Erik
--
Erik Murrey /| // /~~~~/ | /
MPX Data Systems, Inc. / | / / /____/ |/
erik at mpx.com / / / / /| Data Systems, Inc.
{vu-vlsi, bpa, cbmvax}!mpx1!erik / / / / |====================
More information about the Comp.lang.c
mailing list