当我编译并运行我的代码时,我在打印“开始”后立即收到一个总线错误。这是发生的事情:
bash-3.2$ ./remDup
启动
总线错误
#include <stdio.h>
#include <string.h>
void removeDups(char* str)
{
int len = strlen(str);
int i = 0;
for (i = 0; i < len; i++) {
char a = str[i];
int k = i + 1;
int c = 0;
int j = 0;
for (j = k; j < len; j++) {
if (a != str[j]) {
str[k] = str[j];
k++;
} else c++;
}
len -= c;
}
str[len] = '\0';
}
int main(int argc, const char* argv[] )
{
char *str1 = "apple";
printf("%s -> ", str1);
removeDups(str1);
printf("%s\n ", str1);
return 1;
}