指针条件 while(*s1++=*s2++) [英] Pointer Condition while(*s1++=*s2++)

查看:71
本文介绍了指针条件 while(*s1++=*s2++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当这个条件打破时

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

请解释一下逻辑

输出是

推荐答案

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

*s1++, in this variable, * is having higher precedence than ++. In below code,

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返回'\0',分配给s1>.*s1=*s2.现在这个表达式也返回一个值,它是 '\0' 并且 while('\0') 循环终止.
  1. *s1 = *s2 is evaluated first.
  2. Almost every expression in C returns a value. So *s1 = *s2 returns a value which is the first character and its obviously not null. while() loop is executed.
  3. Then s1 and s2 are incremented.
  4. When s2 reaches the end of string, *s2 returns '\0' which is assigned to s1. *s1=*s2. Now this expression also return a value which is '\0' and while('\0') loop terminates.

这篇关于指针条件 while(*s1++=*s2++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆