是,'\0'
NUL char是0
。
要了解的條件表達式考慮我的以下幾點:
一:由於關聯賦值運算符=
是從右到左,因此表達
a = b = c;
是等價於:
a = (b = c);
等效於:
b = c;
a = b;
二:指針賦值表達式:
*s++ = *t++;
是等同於:
*s = *t;
t++;
s++;
三:和表達式:
con = *s++ = *t++;
是等同於:
*s = *t;
con = *s;
t++;
s++;
四:
(*s++ = *t++) != '\0'
是等同於:
*s = *t;
*s != '\0';
t++;
s++;
[答案]:
所以在第二while循環:
通過
t
地址
while (*s++ = *t++);
// ^ ^
// | |
// assign then increments
// then break condition = *s (updated value at *s that is \0 at the end)
指令"*s++ = *t++
副本值由s
地址的值,然後該值變成while循環間隔條件,那麼循環運行,直到發現\0
等於0
。
因此,條件表達式(*s++ = *t++)
相當於(*s++ = *t++) != '\0'
都運行到*s != 0
。
最後同時:
while (s[i] = t[i]) i++;
// ^ ^ ^
// | | increments
// assign then increments
// then break condition = s[i] (s[i] is \0 at the end)
指令的t[i]
到s[i]
s[i] = t[i]
第一副本值然後s[i]
值使用作爲打破的while循環即\0
(= 0)在結束條件。
無論是使用指針還是不使用,''\ 0''都等同於'0'。例如。 ''\ 0'== 0'。閱讀整數促銷章節。 –
@AntonTykhyy,沒有整數推廣發生。 ''0'和'0'都是'int'。好吧,無論如何,在C中。 –
爲什麼這個問題標記爲C++,如果你問C? –