0 insted of '\0' in char ptr

S

sathya

I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.

#include <stdarg.h>
char *xstrcat(char *des, char *src, ...)
{

char *destination = des;
va_list v;
va_start(v, src);
while (src != 0)
{

while (*src != 0)

*des++ = *src++;

src = va_arg(v, char *);

}

*des = 0;
va_end(v);
return destination;
}

Is the above code is UB? Please explain.

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)
 
R

Richard Bos

sathya said:
I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.

That's quite correct. '\0' _is_ an int zero; character constants have
type int, for historical reasons. The necessary conversion to char type
will be done automatically; the process should be identical regardless
of whether you spell the int zero 0 or '\0'.

Richard
 
C

CBFalconer

sathya said:
I have come across the following code which treys to implement the
strcat function. It may be wrong or correct, but it is using
integer zero instead of appending the C string terminator '\0'.

#include <stdarg.h>
char *xstrcat(char *des, char *src, ...)
{
char *destination = des;
va_list v;
va_start(v, src);
while (src != 0)
{
while (*src != 0)
*des++ = *src++;
src = va_arg(v, char *);
}
*des = 0;
va_end(v);
return destination;
}

Is the above code is UB? Please explain.

No it isn't for that reason. However it is a horror and an error
prone trap, as is almost any variadic function in C. You would be
better off using the FreeBSD originated strlcat and strlcpy
routines, for which you can find an implementation at:

<http://cbfalconer.home.att.net/download/>
 
M

Malcolm

sathya said:
I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.
One of the quirks of C is that the string terminator character is
constrained to be zero. So either 0 or '\0' is OK.
What is not OK is what I have seen in some commercial code, which is to
#define EOS_CHAR or similar as zero, then pass EOS_CHAR terminated strings
to standard library functions. This will work, until someone decides that
EOS_CHAR will be control-Z or whatever for compatibility with system X, and
the whole thing breaks.
 
H

Herbert Rosenau

I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.

'\0' is commonly used to document inline that you means a (char)0.
0, char(0), '\0' have always the same meaning but

0 requires from the reader to see that you assigns/compares a char.
The reader has to think an extra step to see that you means a char.

char(0): casting is always an dangerous option, requiring rethinking
it if it does really what you needs and means, so avoiding casts
whenever possible makes the code more readable.

'\0' is documenting that you speaks about chars, not ints. It is more
for the reader than for the compiler.

So both, 0 and '\0' are commonly used to address a null value as char,
whereas '\0' is more documentative but costs 3 extra typings. C
programmers are often sluggish, so they avoid typing as they can.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
R

Richard Bos

Herbert Rosenau said:
'\0' is commonly used to document inline that you means a (char)0.
0, char(0), '\0' have always the same meaning but

No, they don't; 0 has not just exactly the same meaning, but also the
same type, as '\0', but char(0) is a syntax error (and (char)0, which
you might have meant, has the same value, but a different type).

Richard
 

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
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top