Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!ukma!rutgers!deimos!uxc!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: pointers, tests, casts Message-ID: <14849@mimsy.UUCP> Date: 5 Dec 88 06:45:28 GMT References: <11130@dartvax.Dartmouth.EDU> <44100016@hcx3> <9038@smoke.BRL.MIL> <44803@yale-celray.yale.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 68 >In article <9038@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB)) >writes: >>True of pre-ANSI C, but an ANSI C implementation can use either [#define NULL 0] or >>#define NULL ((void*)0) >>I recommend the former even for ANSI C implementations. ... In article <44803@yale-celray.yale.UUCP> wald-david@CS.YALE.EDU (david wald) writes: >I may be sorry in the morning for asking this, but: Perhaps. >Isn't the latter generally preferable, Not particularly. >given its possible use as a parameter for a function with no >prototype in scope? Unless that function takes a `void *' argument in that position, if the NULL is uncast, the code remains incorrect. The compiler cannot diagnose this since the function might take a `void *' argument in that position. For instance: void ** collect(void *first, ...) { va_list ap; int n; void **p, **q, *t; if (first) { va_start(ap, first); n = 2; while (va_arg(ap, void *) != NULL) n++; va_end(ap); } else n = 1; p = malloc(n * sizeof(void *)); if (p == NULL) return (NULL); q = p; if (first) { *q++ = first; va_start(ap, first); while ((t = va_arg(ap, void *)) != NULL) *q++ = t; } *q = NULL; return (p); } Collect() accepts only `void *'; the end of the list it is to collect into a vector is marked by a nil `void *'. (Incidentally, collect() is another example of why requiring an `anchor' argument for va_start is bad.) >Further, isn't the former dangerous in this case, given that there is >no guarantee for NULL and (int)0 to have the same representation? No more dangerous than the latter, given that there is no guarantee for NULL and NULL (of two different types, e.g., `void *' nil and `struct glorp *' nil) to have the same representation. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris