这个程序的函数中两个循环不知道为什么可行

#include<stdio.h>

char *stringcnt(char *dest, const char *source)

{

int i, j;

for(i = 0; dest[i]; i++);

for(j = 0; source[j]; j++)

dest[i++] = source[j];

dest[i] = '\0';

return dest;

}

int main()

{

char a[100] = "C language is my ";

char b[30] = "Favourite language!\n";

printf("%s", stringcnt(a, b));

return 0;

}还有\0的赋值也不懂

\0是结束符
for(i = 0; dest[i]; i++);表示 dest[i]有值存在,循环继续

虽然是两个循环,其实只是执行一次,当拷贝完成时,再次判断i++,判断for(i = 0; dest[i]; i++);中的dest[i]为0,就结束了

dest是个字符串,dest[i]等价于dest[i]!='\0',相当于遍历字符串,直到结尾
也可以改成while循环
结尾赋值成0避免source比dest短,造成dest后半段还保留原来的值
字符串以0作为结尾,后面再有其他字符也不会识别了

第一个for循环没有执行语句,只是将i指向dest的最高位,第二个循环是把source接到dest后面,最后加‘\0’是结束符,表示字符串结束