C语言 斯特伦:它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132849/
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
strlen: how does it work?
提问by Manu
How does strlen()work internally?
strlen()内部如何运作?
回答by paxdiablo
strlenusually works by counting the characters in a string until a \0character is found. A canonical implementation would be:
strlen通常通过计算字符串中的字符直到\0找到一个字符来工作。规范的实现将是:
size_t strlen (char *str) {
size_t len = 0;
while (*str != '##代码##') {
str++;
len++;
}
return len;
}
There is no inherent bug in the function, it works exactly as documented.
该函数没有固有的错误,它完全按照文档工作。
That's not to say it doesn't have problems, to wit:
这并不是说它没有问题,即:
- if you pass it a "string" that doesn't have a
\0at the end, you may run into problems but technically, that's not a C string (a)and it's your own fault. - you can't put
\0characters in your string but, again, it wouldn't be a C string in that case. - it's not the most efficient way - you could store a length up front so you could get the length much quicker.
- 如果您传递给它一个末尾没有 a 的“字符串”
\0,您可能会遇到问题,但从技术上讲,这不是 C 字符串(a),这是您自己的错。 - 您不能
\0在字符串中放入字符,但同样,在这种情况下它不会是 C 字符串。 - 这不是最有效的方法 - 您可以预先存储长度,以便更快地获得长度。
But none of those are bugs, they're just consequences of a design decision.
但这些都不是错误,它们只是设计决策的结果。
See also this excellent articleby Joel Spolsky where he discusses various string formats and their characteristics, including normal C strings, Pascal strings and the combination of the two, null terminated Pascal strings, though he has a more, shall we say, "colorful" term for them :-)
另请参阅Joel Spolsky撰写的这篇优秀文章,他在其中讨论了各种字符串格式及其特征,包括普通 C 字符串、Pascal 字符串以及两者的组合、空终止的 Pascal 字符串,尽管他还有更多,容我们说,“多彩”他们的术语:-)
(a)A C string is defined as a series of non-terminator characters (ie, any other than \0) followed by that terminator. Hence this definition disallows both embedded terminators within the sequence and sequences without such a terminator.
(a)AC 字符串定义为一系列非终止符(即,除 之外的任何字符\0)后跟该终止符。因此,该定义不允许在序列中嵌入终止符和没有这样的终止符的序列。
Or, putting it more succinctly (as per the ISO standard):
或者,更简洁地说(根据 ISO 标准):
A string is a contiguous sequence of characters terminated by and including the first null character.
字符串是由第一个空字符终止并包括第一个空字符的连续字符序列。

