2011-03-26 99 views
2
#include <stdio.h> 
#include <stdlib.h> 
int main(){ 
char *str="abcdce"; 
char c='c'; 
char *pfast=str,*pslow=str; 
while(*pfast!='\0'){ 
    if(*pfast==c){ 
     pfast++; 
     *pslow=*pfast; //error here when pfast reaches the first 'c' 
    } 
    pfast++; 
    pslow++; 
} 
*pslow='\0'; 
return 0; 
} 

當它運行到的賦值語句段故障「* pslow = * pfast;」 ......字符指針賦值段故障

有人告訴我爲什麼,在此先感謝!

+2

您是不是要找™'pslow = pfast;'? – 2011-03-26 13:07:31

回答

8

您正在嘗試更改導致未定義行爲的字符串文字。

變化

char *str="abcdce"; 

char str[] ="abcdce"; 
+0

非常感謝你 – witmusk 2011-03-26 13:10:58

+1

@witmusk:不客氣。由於您是新手,至今未接受任何問題的答案,所以我想告訴您,您可以通過單擊答案旁邊的正確標記來標記幫助您解決問題的答案。 – codaddict 2011-03-26 13:13:17