Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!tut.cis.ohio-state.edu!att!cbnewsc!tjr From: tjr@cbnewsc.ATT.COM (thomas.j.roberts) Newsgroups: comp.lang.c Subject: Re: Is "if (!pointer)" as portable as "if (pointer == NULL)" ??? Message-ID: <15018@cbnewsc.ATT.COM> Date: 11 Apr 90 14:22:18 GMT References: <1461@tkou02.enet.dec.com> Organization: AT&T Bell Laboratories Lines: 24 From article <1461@tkou02.enet.dec.com>, by diamond@tkou02.enet.dec.com (diamond@tkovoa): > In article <656@hades.OZ> greyham@hades.OZ (Greyham Stoney) writes: >> if (buffer = malloc(50)) /* yes, that SHOULD be =, not == */ > > Yes it is portable, just not readable... But beware. In modern compilers (e.g. TURBO C), this will generate a warning (HURRAY!) about a questionable assignment in a conditional context. Avoid the warning with either: if( (buffer=malloc(50)) != 0) [...] or if( !!(buffer=malloc(50)) ) [...] Personally, I either invert the if: buffer = malloc(50); if(!buffer) goto error; /* Yes, it is usually a goto, for there are often dozens of error legs */ Or, more often, supply my own error-checking routine to malloc: extern void *emalloc(size_t n_bytes); buffer = emalloc(50); /* emalloc() bombs on malloc() failure */ Tom Roberts att!ihlpl!tjrob