NULL as a string terminator
Doug Gwyn
gwyn at smoke.BRL.MIL
Sun Aug 5 12:20:26 AEST 1990
In article <26593 at nigel.ee.udel.edu> gdtltr at freezer.it.udel.edu (Gary Duzan) writes:
>=> char command[15];
>=> command[strlen(command)-1]=NULL; /* chop off the \n */
> Pardon me for reopening the NULL discussion, but won't this give a warning
>if NULL is #defined (void *) 0? If it doesn't, should it? Assigning a pointer
>to a char is certainly a questionable thing to do.
You're correct; the example code would happen to work with the traditional
definition of NULL as plain 0, but not if it's defined as ((void*)0). In
general it is a mistake to use the NULL macro for any purpose other than a
stand-in for a null pointer constant. In the example code, either 0 or '\0'
would be a reasonable replacement for NULL.
More information about the Comp.unix.questions
mailing list