C语言 null 终止字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2911089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
null terminating a string
提问by ant2009
gcc 4.4.4 c89
海湾合作委员会 4.4.4 c89
What is the standard way to null terminate a string? When I use the NULLI get a warning message.
null 终止字符串的标准方法是什么?当我使用时,NULL我收到一条警告消息。
*dest++ = 0;
*dest++ = '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;
}
';
*dest++ = NULL; /* Warning: Assignment takes integer from pointer without a cast */
Source code:
源代码:
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;
}
Another 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 NULLor not. i.e. strlen(src) + 1or stlen(src).
另一个问题:我特意注释掉了 null 终止的那一行。但是,它仍然正确打印出 dest 的内容。此函数的调用者将通过包含NULL或不包含来发送字符串的长度。即strlen(src) + 1或stlen(src)。
char *buffer;
...
buffer[end_position] = 'buffer[end_position] = 0;
';
回答by Lucas
To your first question:
I would go with Paul R's comment and terminate with '\0'. But the value 0itself works also fine. A matter of taste. But don't use the MACRO NULLwhich is meant for pointers.
对于您的第一个问题:我会同意 Paul R 的评论并以'\0'. 但是值0本身也很好用。口味问题。但是不要使用NULL用于指针的 MACRO 。
To your second question:
If your string is not terminated with\0, it might still print the expected output because following your string is a non-printable character in your memory. This is a really nasty bug though, since it might blow up when you might not expect it. Alwaysterminate a string with '\0'.
对于您的第二个问题:如果您的字符串没有以 结尾\0,它可能仍会打印预期的输出,因为您的字符串后面是您内存中的不可打印字符。不过,这是一个非常讨厌的错误,因为它可能会在您意想不到的时候爆炸。始终以'\0'.
回答by jamesdlin
From the comp.lang.c FAQ: http://c-faq.com/null/varieties.html
来自 comp.lang.c 常见问题解答:http: //c-faq.com/null/varieties.html
In essence: NULL(the preprocessor macro for the null pointer) is not the same as NUL(the null character).
本质上:(NULL空指针的预处理器宏)与NUL(空字符)不同。
回答by INS
Be verycareful: NULL is a macro used mainly for pointers. The standardway of terminating a string is:
要非常小心:NULL是主要用于指针的宏。终止字符串的标准方法是:
##代码##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.
这(如下)也有效,但将整数值分配给 int/short/long 数组和分配字符值之间没有太大区别。这就是为什么首选第一个版本并且我个人更喜欢它的原因。
##代码##回答by Scott Langham
'\0' is the way to go. It's a character, which is what's wanted in a string and has the null value.
'\0' 是要走的路。它是一个字符,它是字符串中想要的并且具有空值。
When we say null terminated string in C/C++, it really means 'zero terminated string'. The NULL macro isn't intended for use in terminating strings.
当我们在 C/C++ 中说空终止字符串时,它的真正意思是“零终止字符串”。NULL 宏不适用于终止字符串。

