#include <stdio.h> #include <string.h> 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++) { if (strstr(tracks[i], search_for)) printf("Track %i: '%s'\n", i, tracks[i]); } } int main() { char search_for[80]; printf("Search for: "); fgets(search_for, 80, stdin); find_track(search_for); getch(); return 0; }
This is the code obtained directly from Head First C. This does not work. if I change the line in the main
char search_for[80];
to
char *search_for = "town"
This gives me the expected result. I do not understand why this does not work. I understand that directly embedding the code and telling you to find errors here is not very acceptable, but I think it is a very small piece of basic code, so it will do it.
thanks
Hooli source share