我尝试使用makefile在Linux中编译此程序,但输出显示
make: ***[run] segmentation fault
我的代码是:
int removeblanks(char string[], char temp[]){
int i, j=0; printf("no");
while(string[i] != '\0'){
if (string[i] != ' ') {
temp[j] = string[i];
printf("%c\n",temp[j]);
j++;
}
i++;
}
temp[j] = '\0';
return i;
}
int palindromos(int size, char temp[]) {
int i,palindromo=1; int middle= size/2;
for(i=0; i<middle; i++) {
if(temp[i]!=temp[size]){
palindromo= 0;
}
size--;
}
return palindromo;
}
int main() {
char string1[30] ="Anotaram a data da maratona";
char string2[30]="A torre da derrota";
char temp1[30];
char temp2[30];
printf("frase 1: %s\n", string1);
printf("frase 2: %s\n", string2);
int size1=0;
size1=removeblanks(string1,temp1);
printf("size1: %d",size1);
int size2=removeblanks(string1,temp1);
int palindromo1=palindromos(size1, string1);
int palindromo2=palindromos(size2, string2);
printf("As frases sao palindromos.\n");
printf("As frases nao sao palindromos.\n");
return 1;
}
我不知道如何调试并找到错误。有人能帮助我吗?
答案 0 :(得分:1)
在功能int removeblanks(char string[], char temp[])
-
int i, j=0; printf("no"); // i declared not initialized
while(string[i] != '\0'){ // uninitialized local variable used here
...
}
您应该在使用之前初始化i=0
。