看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《CoSNaYe ( ~~)》之銘言: : ==================================================================== : #include <stdio.h> : void strcat_user (char *, char *); : int main(int argc, const char * argv[]) { : char ori[30] = "hello, "; : char add[] = "world."; : strcat_user(ori, add); : //strcat_user(&ori[0], &add[0]); : printf("%s\n", ori); : return 0; : } : void strcat_user (char *ori, char *add){ : while (*ori++) : ; : while ((*ori++ = *add++)) : ; : } : ========================================================== 你的程式在動作後 ori[30] 會存入這些東西 hello, '\0'world.'\0' 但是 printf("%s\n", ori); 遇到hello, 後面的'\0' 就會停止輸出 相信你知道要怎麼修正了:) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.35.118.24 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1420359049.A.528.html
CoSNaYe: 感謝!!我把第一個while 改成while(*ori){ori++}; 就好了 01/04 16:20
narukaze: yup! 01/04 16:21
CoSNaYe: 看來我得善加利用debugger去看的,謝啦! 01/04 16:21
Firstshadow: 看不太懂QQ 01/05 01:09
ACMANIAC: while (*ori++); 導致指標在原字串裡遇到 '\0' 以後又 01/05 13:07
ACMANIAC: 向後移動了一格,之後才接下去做要接上的字串。 01/05 13:07
Firstshadow: 謝謝樓上 我懂了! 01/06 08:39