strlen: how does it work?

你说的曾经没有我的故事 提交于 2019-11-26 20:37:22

问题


How does strlen() work internally?


回答1:


strlen usually works by counting the characters in a string until a \0 character is found. A canonical implementation would be:

size_t strlen (char *str) {
    size_t len = 0;
    while (*str != '\0') {
        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 \0 at 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 \0 characters 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.

But none of those are bugs, they're just consequences of a design decision.

See also this excellent article by 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 :-)


(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.

Or, putting it more succinctly (as per the ISO standard):

A string is a contiguous sequence of characters terminated by and including the first null character.



来源:https://stackoverflow.com/questions/4132849/strlen-how-does-it-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!