鱼C论坛

 找回密码
 立即注册
查看: 2653|回复: 4

[已解决]课后习题有疑问,望答疑。

[复制链接]
发表于 2023-2-7 15:07:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 风归来兮 于 2023-2-7 15:09 编辑

没看懂这个语句是如何实现strncpy的作用的
while ((*target2++ = *target1++) != '\0')
;
最佳答案
2023-2-7 19:08:34
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"
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 19:08:34 | 显示全部楼层    本楼为最佳答案   
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"
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-8 10:46:28 | 显示全部楼层
补充一下,优先级  * > ++  > =  
*target2++ = *target1 ++
等效为
*target2 = *target1;
target1++;
target2++;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-8 20:13:06 | 显示全部楼层
两手空空儿 发表于 2023-2-8 10:46
补充一下,优先级  * > ++  > =  
*target2++ = *target1 ++
等效为

++ > *
https://fishc.com.cn/thread-67664-1-1.html
  1. sh-5.1$ cat main.c
  2. #include <stdio.h>

  3. int main(void) {
  4.     char s0[6] = "hello";
  5.     char s1[6];
  6.     char *target1 = s0;
  7.     char *target2 = s1;
  8.     //while((*target2++ = *target1++) != '\0');
  9.     //while((*(target2++) = *(target1++)) != '\0');
  10.     while(((*target2)++ = (*target1)++) != '\0');
  11.     puts(s1);
  12.     return 0;
  13. }
  14. sh-5.1$ gcc -g -Wall -o main main.c
  15. main.c: In function ‘main’:
  16. main.c:10:25: error: lvalue required as left operand of assignment
  17.    10 |     while(((*target2)++ = (*target1)++) != '\0');
  18.       |                         ^
  19. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-9 09:32:32 | 显示全部楼层
人造人 发表于 2023-2-8 20:13
++ > *
https://fishc.com.cn/thread-67664-1-1.html

好的,谢谢~~~~ 这些个小知识点总是记不清
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表