是的。它必须是一个布尔表达式,可以是任何内部的表达式。
其工作原理如下:
void mystery1(char *s1, const char *s2)
{
while (*s1 != '\0') // NEW: Stop when encountering zero character, aka string end.
s1++;
// NEW: Now, s1 points to where first string ends
for (; *s1 = *s2; s1++, s2++)
// Assign currently pointed to character from s2 into s1,
// then move both pointers by 1
// Stop when the value of the expression *s1=*s2 is false.
// The value of an assignment operator is the value that was assigned,
// so it will be the value of the character that was assigned (copied from s2 to s1).
// Since it will become false when assigned is false, aka zero, aka end of string,
// this means the loop will exit after copying end of string character from s2 to s1, ending the appended string
; // empty statement
}
}
这样做是从S2的所有字符复制到S1末端,基本上追加S2到S1。
要明确,\n
与此代码无关。
来源
2009-10-22 18:05:23
DVK
+1击败了我。 – Tom 2009-10-22 18:08:04
难道它不取决于运营商的返回类型=? (是的,在这里我们正在处理内置函数,但是有一个自定义类...) – 2009-10-22 19:13:53
@Matthieu M. C/C++中的表达式总是评估一些东西。这适用于用户定义类型或内置类型的值:struct myclass {}; int main(){myclass(); }'。我对第一句话中的“总是”并不是100%确定的,但是我对C/C++语句的理解。 – AraK 2009-10-22 19:19:00