我有以下功能,它是 Trie 结构实现的一部分:
int alpha_char_strlen (const AlphaChar *str) {
const AlphaChar *p;
for (p = str; *p; p++) ;
return p - str;
谁能帮我解释一下 for 循环的条件是如何成立的,以及这种情况下的条件究竟是什么?
注意:AlphaChar 只是一个 unsigned int 类型的 typedef,该函数对 AlphaChar 字符进行计数。
最佳答案
条件 *p
与 *p != 0
相同(如果 *p
是原始类型)。也就是说,您不断递增指针 p
直到它指向零。换句话说,您将 str
视为指向零终止数组的指针。
关于c++ - 这个条件如何成立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916020/