我是初学者,tracks.c:
#include <stdio.h>
#include <string.h>
char tracks[][5] = {
"one",
"two",
"three",
"four",
"five",
"six",
};
void track_search(char search_for[]) {
int i;
puts(search_for);
puts(strstr(tracks[0], search_for));
/*
for (i = 0; i < 6; i++) {
if (strstr(tracks[i], search_for)) {
printf("tracks %i: %s\n", i,tracks[i]);
} else {
puts("Nothing found");
}
}
*/
}
int main() {
char search_for[5];
printf("enter your word: ");
fgets(search_for, 5, stdin);
track_search(search_for);
return 0;
}
$ gcc track.c && ./a.out
输入你的话:开
上
分段故障
但是如果我使用 puts(strstr(tracks[0], "on")); 而不是 puts(strstr(tracks[0], search_for)); 可以用酒,有人知道哪里错了吗?