我正在尝试编写自己的strtok函数。
char * toke(char * out, char * in, char * destr) {
int place = 0;
for(int i = 0; in[i] != '\0'; i++){
for(int d = 0; destr[d] != '\0'; d++){
if(in[i] == destr[d]){
printf("\nMatch.");
place = i;
i = 0;
while(i < place){
out[i] = in[i];
i++;
}
out[place + 1] = '\0';
i = place;
}
}
}
return out;
}
程序:
int main(){
char * pr;
char * o;
char * in = "Hello-World";
pr = toke(o, in, "-");
printf(pr);
return 0;
}
它继续发生分段错误,我无法弄清楚原因。
答案 0 :(得分:0)
您的'out'指针未初始化,并且:
out[i] = in[i];
抛出异常