我的印象是 for 循环必须包括:for(init; condition; increment)
现在我遇到了这段代码:
char *foo, *bar;
char* data = getData();
for (foo = data; *foo; foo = bar + 1) {
for (bar = foo; *bar; ++bar) {
if (bar >= &data[1024]) {
printf("error\n");
}
printf("%s\n", foo);
}
指针的取消引用是有效条件吗?如果是,它什么时候返回 false
?什么时候指向NULL?
最佳答案
Is the dereferencing of a pointer a valid condition?
在 C 和 C++ 中,任何可以在给定上下文中转换为 bool
的表达式都可以用作循环或条件语句中的有效条件。表达式 *foo
生成类型为 char
的值,该值可转换为 bool
。
when does it return false?
当表达式的计算结果为零时。所有其他值都被视为 true
。
When it points to
NULL
?
foo
是一个 char*
,所以当 *foo
是 时条件变为
false
'\0'
关于c++ - 将指针取消引用作为 for 循环条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24761235/