Header problems

paul pedersen pedersen at acf3.NYU.EDU
Thu Mar 10 11:20:41 AEST 1988


In article <10574 at mimsy.UUCP> chris at 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.)



More information about the Comp.lang.c mailing list