char tracks[][80] = {
"I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};
void find_track(char search_for[]) {
int i;
for (i = 0; i < 5; i++) {
char *pointer = strstr(tracks[i], search_for);
printf("P:%s\n", pointer);
}
}
int main() {
char search_for[80];
printf("Search for: ");
fgets(search_for, 80, stdin);
// scanf("%79s", search_for);
find_track(search_for);
return 0;
}
That's a example for the usage of strstr() and fgets(). But when I run it, it cannot return the matched pointer, it's always the null.
Search for: wonderful
P:(null)
P:(null)
P:(null)
P:(null)
P:(null)
And I tried some different expressions: 1.I reduced the capacity of the search_for array from 80 to 10:
char search_for[10];
it works!
Search for: wonderful
P:(null)
P:wonderful town
P:(null)
P:(null)
P:(null)
2.I replaced the function fgets() by scanf().
scanf("%79s", search_for);
it also works!
Search for: wonderful
P:(null)
P:wonderful town
P:(null)
P:(null)
P:(null)
Therefore, I am confused about it, and anyone can tell me the deep reason about the phenomenon?