我看到有人发布了此函数,该函数返回字符串的长度。有人可以逐行向我解释发生的情况是因为我不了解* s指针发生了什么,以及它如何能够一一遍历字符串并计算字符串中的字符数。该功能来自FreeBSD
size_t
strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s);
return(s - str);
}
最佳答案
size_t
strlen(const char *str)
{
const char *s; // init pointer
for (s = str; *s; ++s); // set pointer to beginning of str, and increment pointer until
// you reach '\0', which is the end of the string
return(s - str); // compute the distance between end and beginning of string
// (s points to end of string, str points to beginning of string)
}
关于c - 返回字符串的长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33046329/