在C中的while循环中使用指针进行后递增
发布时间:2020-12-16 10:37:36 所属栏目:百科 来源:网络整理
导读:我正在编写strcat函数 /*appends source string to destination string*/#include stdio.hint main(){ char srcstr[100],deststr[100]; char *psrcstr,*pdeststr; printf("n Enter source string: "); gets(srcstr); printf("n Enter destination string:
我正在编写strcat函数
/*appends source string to destination string*/ #include <stdio.h> int main() { char srcstr[100],deststr[100]; char *psrcstr,*pdeststr; printf("n Enter source string: "); gets(srcstr); printf("n Enter destination string: "); gets(deststr); pdeststr = deststr; psrcstr = srcstr; while(*pdeststr++) ; while(*pdeststr++ = *psrcstr++) ; printf("%s",deststr); return 0; } 对于srcstr =“world”和deststr =“hello”我得到你好,当我希望看到hello world时,如果我改变了第一个,那就是我所看到的 while(*pdeststr); pdeststr++; 为什么我不能在第一行中写入所有内容,就像在第二行中一样? 解决方法
强制性介绍:不要使用gets(),使用fgets()!
你的问题在这里: while(*pdeststr++) ; 递增的副作用是在最后一次迭代步骤中执行的(当pdeststr指向NUL终止符时),因此在此循环之后,pdeststr在NUL终止符之后指向一个.写它像这样: while(*pdeststr) ++pdeststr; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |