Why (long)NULL ?

S

sugaray

Hi, I just came upon this code snippet which parses a string stored in buf
with comma(,) as delimiter and store each substring into args, the question
I'm having here is that I don't get why in the first while-statement the OP
cast the NULL to a (long), isn't *buf a char ? and another question is what's
the purpose of NULL in string manipulation, and how to test to see if an input
string is empty ? Thanx for your help.

void parse (char *buf, char **args) {

while (*buf != (long) NULL ) {

while ( *buf == ',' )
*buf++ = (long) NULL ;

*args++ = buf;

while ( (*buf != (long) NULL) && (*buf != ',') )
buf++;

}
*args = NULL;

}
 
B

Ben Pfaff

Hi, I just came upon this code snippet which parses a string stored in buf
with comma(,) as delimiter and store each substring into args, the question
I'm having here is that I don't get why in the first while-statement the OP
cast the NULL to a (long), isn't *buf a char ? and another question is what's
the purpose of NULL in string manipulation, and how to test to see if an input
string is empty ? Thanx for your help.

void parse (char *buf, char **args) {

while (*buf != (long) NULL ) {

while ( *buf == ',' )
*buf++ = (long) NULL ;

*args++ = buf;

while ( (*buf != (long) NULL) && (*buf != ',') )
buf++;

}
*args = NULL;

}

It looks like this code was written by someone more or less
illiterate in C. As you say, writing "(long) NULL" does not make
sense. In the above code, each instance of it should be replaced
by '\0'.

NULL doesn't really have much to do with strings. If you want to
test whether a string is empty, check whether the first byte in
it is a zero byte (e.g. *string == '\0').
 
R

Richard Heathfield

sugaray said:
Hi, I just came upon this code snippet which parses a string stored in buf
with comma(,) as delimiter and store each substring into args, the
question I'm having here is that I don't get why in the first
while-statement the OP cast the NULL to a (long), isn't *buf a char ? and
another question is what's the purpose of NULL in string manipulation, and
how to test to see if an input string is empty ? Thanx for your help.

void parse (char *buf, char **args) {

Why yes! *buf /is/ a char.
while (*buf != (long) NULL ) {

What utter confusion. *buf is a char. NULL is a null pointer constant. What
he meant to say was *buf != '\0' -- both the NULL /and/ the cast are a
result of his confusion about what he's trying to do.
while ( *buf == ',' )
*buf++ = (long) NULL ;

Same deal. This should be: *buf++ = '\0';
*args++ = buf;

while ( (*buf != (long) NULL) && (*buf != ',') )

And again: *buf != '\0'
 
G

Greg Barron

Hi.

The NULL is being cast to long as the writer of this code could not figure
out what they did wrong and the cast probably shut the compiler up. NULL
is generally used for pointer values of zero, not for character values of
zero. Hence the cast.

To be correct this code should really compare against '\0' which is a
character zero. I normally define and use NUL to represent this.

Cheers,
 
M

Mike Wahler

Richard Heathfield said:
Why yes! *buf /is/ a char.


What utter confusion. *buf is a char. NULL is a null pointer constant. What
he meant to say was *buf != '\0' -- both the NULL /and/ the cast are a
result of his confusion about what he's trying to do.

Possible cause of confusion is the ASCII notation for
zero char is 'NUL' combined with assumption of ASCII
character set. Of course no relation to C's NULL.

The frequency with which such code appears is kinda scary. :)


-Mike
 
R

Richard Heathfield

Mike said:
Possible cause of confusion is the ASCII notation for
zero char is 'NUL' combined with assumption of ASCII
character set.

Another possible cause of confusion is the EBCDIC
notation for zero char is 'NUL' combined with assumption
of EBCDIC character set.
Of course no relation to C's NULL.
Right.

The frequency with which such code appears is kinda scary. :)

What is even scarier is the frequency with which people
write such code, even when they have been shown that it's
wrong and how they can write a correct equivalent.
 
D

Dan Pop

If you want to learn C this way, don't pick random pieces of code. Ask
an experienced programmer to recommend you some well written C source
code and use it, instead.

It serves no purpose at all: if a pointer compares equal to NULL, it
cannot, by definition, be used in any string manipulation. Unless you
define your convention to treat null pointers as pointers to empty
strings (which is not a particularly good idea).

I'm sorry, but this question is too basic for c.l.c. If you can't find
the answer in your C book, switch to BASIC: C is too difficult for you.
Why yes! *buf /is/ a char.


What utter confusion. *buf is a char. NULL is a null pointer constant. What
he meant to say was *buf != '\0' -- both the NULL /and/ the cast are a
result of his confusion about what he's trying to do.

However, more by accident than by design, they get the job done (with
the exception of the hypothetical implementation where (long) NULL doesn't
yield 0L).

Dan
 
D

Dan Pop

Possible cause of confusion is the ASCII notation for
zero char is 'NUL' combined with assumption of ASCII
character set. Of course no relation to C's NULL.

This still doesn't explain the cast to long. We need to speculate
further: NULL expanded to (void *)0 on his compiler and pointers could be
(silently) converted only to longs. So, instead of writing 0, the
idiot invented (long) NULL for this purpose.

Dan
 
M

Michael Wojcik

Another possible cause of confusion is the EBCDIC
notation for zero char is 'NUL' combined with assumption
of EBCDIC character set.

Another is the Standard's use of the phrase "null character" to refer
to '\0'. I can understand someone being confused by the distinction
between "null character constant" and "null pointer constant", and
the latter's relationship to the "NULL" macro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top