该程序可以编译,但是当运行时输出 find() 方法中的一些第一个打印语句,然后Segmentation fault:11
在 for 循环命中时发出。
所以我尝试使用 gdb 进行调试,但无法弄清楚问题所在。根据我从 gdb 收集的信息,由于 strstr() 方法之一发生了段错误。另外,我注意到程序在调用 find 方法中的 for 循环之前关闭。为什么这个程序可以编译但在运行时崩溃?
这是 gdb 的输出:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000001
0x00007fff8d6469c4 in strstr ()
(gdb)
感谢您的所有帮助。我实际上是 C 的完整初学者,但我正在努力学习更多。
#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 and TV",
"Beavis likes Metallica and TV",
"Cartman likes to eat",
"Spiderman likes theater and working-out",
"Silver Surfer likes surfing and space",
"GunSword likes mustard and 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 this line...");
puts("-----------------------------------------------");
for (i = 0;i < NUM_STUFF; i++) {
if (match(STUFF[i])) {
printf("%s\n", STUFF[i]);
}
}
puts("-----------------------------------------------");
}