课后习题有疑问,望答疑。
本帖最后由 风归来兮 于 2023-2-7 15:09 编辑没看懂这个语句是如何实现strncpy的作用的
while ((*target2++ = *target1++) != '\0')
; while ((*target2++ = *target1++) != '\0')
设:
*target2 == "ab\0"
*target1 == "ac\0"
1 -> *target1的值'a'赋值给*target2,得*target2='a' (target2处的值被赋值为'a'), ('a')!='\0'?随后target1,target2地址后移。
2 ->*target1的值'c'赋值给*target2,得*target2='c' (target2处的值被赋值为'c') ('c')!='\0'?随后target1,target2地址后移。
3 ->*target1的值'\0'赋值给*target2,得*target2='\0' (target2处的值被赋值为'\0') ('\0')!='\0'?随后target1,target2地址后移。
4 -> 注: 最后原target2处理的字符串会被替换为"ac\0" 补充一下,优先级* > ++> =
*target2++ = *target1 ++
等效为
*target2 = *target1;
target1++;
target2++; 两手空空儿 发表于 2023-2-8 10:46
补充一下,优先级* > ++> =
*target2++ = *target1 ++
等效为
++ > *
https://fishc.com.cn/thread-67664-1-1.html
sh-5.1$ cat main.c
#include <stdio.h>
int main(void) {
char s0 = "hello";
char s1;
char *target1 = s0;
char *target2 = s1;
//while((*target2++ = *target1++) != '\0');
//while((*(target2++) = *(target1++)) != '\0');
while(((*target2)++ = (*target1)++) != '\0');
puts(s1);
return 0;
}
sh-5.1$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:10:25: error: lvalue required as left operand of assignment
10 | while(((*target2)++ = (*target1)++) != '\0');
| ^
sh-5.1$
人造人 发表于 2023-2-8 20:13
++ > *
https://fishc.com.cn/thread-67664-1-1.html
好的,谢谢~~~~ 这些个小知识点总是记不清{:5_99:}
页:
[1]