[英]What's the meaning of "&&" when operated by a char * on a char in C++?
I came across a code which extracted a token(c string) from a given string,but I am confused by the 5th line: tok && *tok
.我遇到了从给定字符串中提取令牌(c 字符串)的代码,但我对第 5 行感到困惑:
tok && *tok
。 I just cannot figure out this operation's result.我就是想不通这个操作的结果。 Does anybody happen to know?
有人碰巧知道吗?
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
Thank you!谢谢你!
它首先检查指针( tok
)是否不是nullptr
(指向nullptr
),然后如果指针不是nullptr
,它将检查它是否指向值( *tok
)不是'\\0'
内存。
This is the conditional expression, and it is checking that tok
is not nullptr
and that *tok
, or the character, is not the "nul" character '\\0'
. 这是条件表达式,它正在检查
tok
不是nullptr
,并且*tok
或字符不是“ nul”字符'\\0'
。
In a C-style for-loop, you have the initalizer, the condition, and the increment expressions. 在C样式的for循环中,您具有初始化器,条件和增量表达式。 The condition here is that
tok
evaluates to true, then *tok
evaluates to true. 这里的条件是
tok
评估为true,然后*tok
评估为true。
This can only occur if tok
is not nullptr
, and the character pointed to ( *tok
) is not '\\0'
. 仅当
tok
不是nullptr
且指向( *tok
)的字符不是'\\0'
,才会发生这种情况。
The meaning of && remains the same. &&的含义保持不变。 It is still a logical operator.
它仍然是一个逻辑运算符。 That statement is equivalent to
那句话等于
(tok != nullptr) && (*tok != '\0');
Can any point out what the below portion means?谁能指出下面的部分是什么意思?
{
if (!--num)
return tok;
}
I'm most interested in the if (!--num)
.我对
if (!--num)
最感兴趣。 It's not making sense to me.这对我来说没有意义。 In my head it reads as
if NOT num = num - num.
在我的脑海里,它看起来
if NOT num = num - num.
It's really cramping my brain.真让我脑袋抽筋。
I know, is the NOT operator.我知道,是 NOT 运算符。 but the --num just doesn't compute in my brain.
但是 --num 只是不在我的大脑中计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.