null terminating a string

Posted by robUK on Stack Overflow See other posts from Stack Overflow or by robUK
Published on 2010-05-26T08:02:53Z Indexed on 2010/05/26 8:31 UTC
Read the original article Hit count: 190

Filed under:

Hello,

gcc 4.4.4 c89

just wondering what is the standard way to null terminate a string. i.e. However, when I use the NULL I get the warning message.

*dest++ = 0; 
*dest++ = '\0'; 
*dest++ = NULL; /* Warning: Assignment takes integer from pointer without a cast */

source code I am using:

size_t s_strscpy(char *dest, const char *src, const size_t len)
{
    /* Copy the contents from src to dest */
    size_t i = 0;
    for(i = 0; i < len; i++)
    *dest++ = *src++;

    /* Null terminate dest */
     *dest++ = 0; 

    return i;
}

Just another quick question. I deliberately commented out the line that null terminates. However, it still correctly printed out the contents of the dest. The caller of this function would send the length of the string by either included the NULL or not. i.e. strlen(src) + 1 or stlen(src).

size_t s_strscpy(char *dest, const char *src, const size_t len)
{
    /* Copy the contents from src to dest */
    size_t i = 0;
    /* Don't copy the null terminator */
    for(i = 0; i < len - 1; i++)
    *dest++ = *src++;

    /* Don't add the Null terminator */
    /* *dest++ = 0; */

    return i;
}

Many thanks for any advice,

© Stack Overflow or respective owner

Related posts about c