Path: utzoo!mnetor!uunet!husc6!cmcl2!acf3!pedersen From: pedersen@acf3.NYU.EDU (paul pedersen) Newsgroups: comp.lang.c Subject: Re: Header problems Message-ID: <596@acf3.NYU.EDU> Date: 10 Mar 88 01:20:41 GMT References: <2550049@hpisod2.HP.COM> <7412@brl-smoke.ARPA> <3351@chinet.UUCP> <10574@mimsy.UUCP> Reply-To: pedersen@acf3.UUCP (paul pedersen) Organization: New York University Lines: 34 In article <10574@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >Finally, note that in this last example, NO SINGLE DEFINITION OF NULL >can make the two lines marked `wrong' correct: > > main() { > f1(NULL); /* wrong */ > f2(NULL); /* wrong */ > exit(0); > } > > f1(cp) char *cp; { if (cp != NULL) *cp = 'a'; } > f2(dp) double *dp; { if (dp != NULL) *dp = 2.2; } > >The only way to fix this last example is by adding prototypes and/or >casts. I have settled on the following approach, abandoning NULL altogether: # define NIL(TYPE) ((TYPE *)0) and now refer to all null pointers as NIL(char), NIL(int), NIL(struct foo), etc. Similarly, I use the following macros for allocating/freeing: char *malloc(); char *free(); # define ALLOC(TYPE) ((TYPE *) malloc(sizeof(TYPE))) # define FREE(p) (free((char *) p)) This syntax is pre-ANSI, but I don't see why it shouldn't work even post-ANSI, provided the declarations are changed to proper prototypes, and (void *) is introduced as needed. I have come to feel that the presence of an explicit cast in the code is bletcherous, similar to the presence of an explicit manifest constant. (For people who need to allocate vectors, a CALLOC macro with args TYPE and n is also probably in order; I haven't had a use for this yet.)