作者PiscesGold (青山依舊在﹐幾度夕陽紅)
看板Programming
標題Re: [問題]關於strcpy()
時間Thu Apr 19 20:10:31 2007
因為 strcpy(s, "z"); 之後
s[0] = 'z'
s[1] = '\0' // 這個是 strcpy 作用的結果
s[2] = 'n'
...
printf 碰到 '\0' 就停住了
※ 引述《[email protected] (漢唐中醫支持者)》之銘言:
: #include<stdio.h>
: #include<string.h>
: int main()
: {
: char s[10]="abm";
: printf("%s\n",s);
: strcpy(s,"z");
: printf("%s\n",s);
: printf("%c%c%c",s[0],s[1],s[2]);
: return 0;
: }
: -
: 結果:
: abm
: z
: z m
: -
: 為什麼第二行不會印出z m呢?反而到第三行才印出來?
: s[2]在第二行消失了嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.194.15.150