C 自定义strtok函数未按预期工作
我试图编写自己的strtok函数C 自定义strtok函数未按预期工作,c,strtok,C,Strtok,我试图编写自己的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.");
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;
}
它不断出现seg故障,我不知道为什么。您的“out”指针未初始化,这:
out[i] = in[i];
抛出异常您应该进行一些调试。1)
char*o代码>-->charo[16]代码>2)out[place+1]='\0'代码>-->out[i]='\0'代码>谢谢你让它工作了3)逻辑整体上可能是错误的。顺便说一句:我想你可以用for(inti=0;;I++){for(intd=0;;d++){
作为最终的在[I]
中匹配destr[d]
当两者都是'\0'
时。
out[i] = in[i];