Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!polyslo!vlsi3b15!mpx1!mpx2!erik From: erik@mpx2.mpx.com (Erik Murrey) Newsgroups: comp.lang.c Subject: use of if (!cptr) and if (cptr), where cptr is a * Keywords: * != int Message-ID: <10099@mpx2.mpx.com> Date: 17 Jul 89 12:29:41 GMT Reply-To: erik@mpx2.mpx.com (Erik Murrey) Organization: MPX Data Systems, Inc. , Wayne, PA Lines: 48 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@mpx.com / / / / /| Data Systems, Inc. {vu-vlsi, bpa, cbmvax}!mpx1!erik / / / / |====================