i'm a beginner,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 tracks.c && ./a.out
enter your word: on
on
Segmentation fault
but if I use puts(strstr(tracks[0], "on")); instead of puts(strstr(tracks[0], search_for)); it will works wine,anyone knows where is wrong?