バス エラーが発生するのはなぜですか? 問題のある行はコード内にマークされています。
演習 2-4. 文字列 s2 の任意の文字に一致する s1 の各文字を削除する、squeeze(s1,s2) の代替バージョンを作成します。
#include <stdio.h>
/*
* Detects if a char is inside a string
*/
char in_string(char c, char s[]) {
int i = 0;
while (s[i] != '\0') {
if (s[i++] == c)
return 1;
}
return 0;
}
/*
* Returns the string s without any chars that are in map
*/
void squeeze(char s[], char map[]) {
int i, j;
for (i = j = 0; s[i] != '\0'; i++) {
if (! in_string(s[i], map)) {
s[j++] = s[i]; // <--- Bus Error
}
}
s[j] = '\0';
printf("%s\n", s);
}
main() {
squeeze("XALOMR", "AO");
squeeze("EWRTOG", "RGV");
}