0 insted of '\0' in char ptr

Discussion in 'C Programming' started by sathya, Dec 14, 2004.

  1. sathya

    sathya Guest

    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 = .)
     
    sathya, Dec 14, 2004
    #1
    1. Advertisements

  2. sathya

    Richard Bos Guest

    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
     
    Richard Bos, Dec 14, 2004
    #2
    1. Advertisements

  3. sathya

    CBFalconer Guest

    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/>
     
    CBFalconer, Dec 14, 2004
    #3
  4. sathya

    Malcolm Guest

    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.
     
    Malcolm, Dec 14, 2004
    #4
  5. '\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!
     
    Herbert Rosenau, Dec 15, 2004
    #5
  6. sathya

    Richard Bos Guest

    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
     
    Richard Bos, Dec 15, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.