null terminating a string

前端 未结 4 742
有刺的猬
有刺的猬 2020-11-29 05:48

gcc 4.4.4 c89

What is the standard way to null terminate a string? When I use the NULL I get a warning message.

*dest++ = 0; 
*dest++ =          


        
4条回答
  •  孤独总比滥情好
    2020-11-29 06:35

    Be very careful: NULL is a macro used mainly for pointers. The standard way of terminating a string is:

    char *buffer;
    ...
    buffer[end_position] = '\0';
    

    This (below) works also but it is not a big difference between assigning an integer value to a int/short/long array and assigning a character value. This is why the first version is preferred and personally I like it better.

    buffer[end_position] = 0; 
    

提交回复
热议问题