小编Hug*_*ugo的帖子

在C中的while循环中使用指针发布增量

我正在写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;
}
Run Code Online (Sandbox Code Playgroud)

对于srcstr = " world"deststr = "hello"我得到的hello,我希望看到hello world,这是我所看到的,如果我第一次改变while这样

while(*pdeststr);
    pdeststr++;
Run Code Online (Sandbox Code Playgroud)

为什么我不能在第一行中写所有内容while,就像在第二行一样while

c c-strings

4
推荐指数
1
解决办法
1841
查看次数

标签 统计

c ×1

c-strings ×1