gdb - c program seg fault not making sense to me (c beginner) -
this program compiles when run outputs few of first print statements in find() method issues segmentation fault:11
when loop hits.
so tried debugging gdb not figure out problem is. gathered gdb seg fault occurs because of 1 of strstr() methods. also, noticed program shuts-down right before call loop in find method. can tell me why program compiles crashes @ run-time?
here output gdb:
program received signal exc_bad_access, not access memory. reason: kern_invalid_address @ address: 0x0000000000000001 0x00007fff8d6469c4 in strstr () (gdb)
thanks , help. complete beginner practically c, trying learn more.
/* * * */ #include <stdio.h> #include <string.h> int acdc_or_metallica(char *s); int surfing_or_tv(char *s); int exo_mustard(char *s); int arts_theater_or_dining(char *s); void find(int(*match)(char*)); int num_stuff = 7; char *stuff[] = { "butthead likes acdc , tv", "beavis likes metallica , tv", "cartman likes eat", "spiderman likes theater , working-out", "silver surfer likes surfing , space", "gunsword likes mustard , exoskeletons" "meatwad likes tv" }; int main() { find(acdc_or_metallica); find(surfing_or_tv); find(exo_mustard); find(arts_theater_or_dining); return 0; } int acdc_or_metallica(char *s) { return strstr(s, "acdc") || strstr(s, "metallica"); } int surfing_or_tv(char *s) { return strstr(s, "surfing") || strstr(s, "tv"); } int exo_mustard(char *s) { return strstr(s, "exoskeleton") && strstr(s, "mustard"); } int arts_theater_or_dining(char *s) { return strstr(s, "arts") || strstr(s, "theater") || strstr(s, "dining"); } void find(int(*match)(char*)) { int i; puts("search results:"); puts("error below line..."); puts("-----------------------------------------------"); (i = 0;i < num_stuff; i++) { if (match(stuff[i])) { printf("%s\n", stuff[i]); } } puts("-----------------------------------------------"); }
char *stuff[] = { "butthead likes acdc , tv", "beavis likes metallica , tv", "cartman likes eat", "spiderman likes theater , working-out", "silver surfer likes surfing , space", "gunsword likes mustard , exoskeletons" //<---- , "meatwad likes tv" };
you forgot "," because data not enough
Comments
Post a Comment