小编wit*_*usk的帖子

字符指针分配段故障

#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;
}
Run Code Online (Sandbox Code Playgroud)

当它运行到"*pslow =*pfast;"的赋值语句时出现段错误...

有人告诉我为什么,提前谢谢!

c string variable-assignment segmentation-fault

2
推荐指数
1
解决办法
662
查看次数