将指针解引用为循环条件? [英] Dereferencing a pointer as for-loop condition?
问题描述
for(init; condition; increment)
现在我遇到了这个代码: p>
char * foo,* bar;
char * data = getData();
for(foo = data; * foo; foo = bar + 1){
for(bar = foo; * bar; ++ bar){
if(bar> ; =& data [1024]){
printf(error\\\
);
}
printf(%s\\\
,foo);
}
指针的引用是否为有效条件?如果是,什么时候返回 false
?当它指向NULL?
指针的引用是否有效? p>
在C和C ++中,任何可以转换为 bool
给定上下文可以用作循环或条件语句中的有效条件。表达式 * foo
生成 char
类型的值,可转换为 bool
。
何时返回false?
当表达式求值为零时。所有其他值都被视为 true
。
c> NULL ?
foo
char *
,因此当 * foo
时,条件变为 false
是'\0'
I was under the impression a for loop has to consist of: for(init; condition; increment)
Now I came across this code:
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);
}
Is the dereferencing of a pointer a valid condition? If so, when does it return false
? When it points to NULL?
Is the dereferencing of a pointer a valid condition?
In C and C++, any expression that can be converted to a bool
in a given context can be used as a valid condition in a loop or a conditional statement. An expression *foo
produces a value of type char
, which is convertible to bool
.
when does it return false?
When the expression evaluates to zero. All other values are considered true
.
When it points to
NULL
?
foo
is a char*
, so the condition becomes false
when *foo
is '\0'
这篇关于将指针解引用为循环条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!