指针条件 while(*s1++=*s2++) [英] Pointer Condition while(*s1++=*s2++)
本文介绍了指针条件 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.
*s1 = *s2
首先评估.- C 中几乎每个表达式都返回一个值.所以
*s1 = *s2
返回一个值,它是第一个字符,显然不是空的.while()
循环被执行. - 然后
s1
和s2
递增. - 当
s2
到达字符串末尾时,*s2
返回'\0'
,分配给s1
>.*s1=*s2
.现在这个表达式也返回一个值,它是'\0'
并且while('\0')
循环终止.
*s1 = *s2
is evaluated first.- 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. - Then
s1
ands2
are incremented. - When
s2
reaches the end of string,*s2
returns'\0'
which is assigned tos1
.*s1=*s2
. Now this expression also return a value which is'\0'
andwhile('\0')
loop terminates.
这篇关于指针条件 while(*s1++=*s2++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文