c - 指针条件 while(*s1++=*s2++)

标签 c pointers

int main()
{
char str1[] = "Overflow";
char str2[] = "Stack";
char *s1 = str1, *s2=str2;
while(*s1++ = *s2++)
printf("%s", str1);
return 0;
}

当这个条件被打破时

(while(*s1++ = *s2++))

请解释一下逻辑

输出结果是

enter image description here

最佳答案

*s1++,在这个变量中,*的优先级高于++。 在下面的代码中,

while(*s1++ = *s2++); //There should be a semicolon.Your code does not has.
  1. *s1 = *s2 首先计算。
  2. C 中几乎每个表达式都会返回一个值。所以 *s1 = *s2 返回一个值,它是第一个字符,它显然不为空。 while() 循环被执行。
  3. 然后s1s2递增。
  4. s2到达字符串末尾时,*s2返回赋给s1'\0' >。 *s1=*s2。现在这个表达式也返回一个值,它是 '\0' 并且 while('\0') 循环终止。

关于c - 指针条件 while(*s1++=*s2++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19995338/

相关文章:

c - 如何在 C 中获取 array[] 的长度?

c - 将包含指针的结构写入磁盘

c - Asterisk AGI 与 C 问题

c++ - 汽车和引用

c - 文字中的非 ASCII、非 Unicode 字符

c 中无法返回负值

c++ - 读取访问冲突 0xCDCDCDCD

c++ - 使用 %u 读取签名的字符

ios - 按下按钮后记录时数据 (char*) 出现损坏

swift - 在 Swift 的 SQLite 中存储指向对象的指针