[comp.unix.questions] NULL as a string terminator

gdtltr@freezer.it.udel.edu (Gary Duzan) (08/04/90)

In article <707@mtune.ATT.COM> jrw@mtune.ATT.COM (Jim Webb) writes:
=>
=>	char psbuf[64];
=>	char command[15];
=>	FILE *ps;
=>	
=>	/* I'm sure this could be done better in perl :-) */
=>	sprintf(psbuf,"ps -p%d|awk '{/PID/ {next} { print $4 }'",getpid());
=>	ps=popen(psbuf,"r");
=>	fgets(command,15,ps);
=>	pclose(ps);
=>	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.

                                        Gary Duzan
                                        Time  Lord
                                    Third Regeneration



--
                          gdtltr@freezer.it.udel.edu
   _o_                    --------------------------                      _o_
 [|o o|] If you can square, round, or cube a number, why not sphere it? [|o o|]
  |_O_|         "Don't listen to me; I never do." -- Doctor Who          |_O_|

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/05/90)

In article <26593@nigel.ee.udel.edu> gdtltr@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.