#include <stdio.h>
#include <string.h>
int scomp(char* x, char* y);
int main(void)
{
printf("gimme a string: ");
char* str1;
scanf("%s",str1);
printf("gimme another string: ");
char* str2;
scanf("%s",str2);
printf("comparing them......\n__________\n");
if(scomp(str1,str2) == 0)
{
printf("yup, they are the same\n");
}else if(scomp(str1,str2)== 1){
printf("They are different buddy..\n");
}
return 0;
}
int scomp(char* x, char* y)
{
int n=0;
while(x[n] != '\0')
{
if(x[n] == y[n])
{
n++;
}else{
return 1;
}
}
return 0;
}
它給了我分割故障發生11,一定有什麼毛病我這是應該比較字符串的最後一部分寫的功能。 有什麼問題?我不明白爲什麼分段錯誤在我的C代碼
你是什麼scanfs的返回值,並根據是什麼意思規格? – Yunnosch
您正在將數據讀入未分配的內存('str1'和'str2'),這不是一個好主意。但是你的代碼也存在邏輯問題,但不會導致seg錯誤。 – AntonH
str1和str2指向什麼? – Warrior