c程序段错误对我没有意义(c初学者)

时间:2013-05-14 00:08:59

标签: c gdb segmentation-fault

这个程序编译但是当运行输出find()方法中的一些第一个print语句时,当for循环命中时会发出Segmentation fault:11

所以我尝试用gdb进行调试,但无法弄清问题是什么。根据我从gdb收集的内容,由于strstr()方法之一而发生seg错误。另外,我注意到程序在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("-----------------------------------------------");
}

2 个答案:

答案 0 :(得分:4)

您正在使用match指针调用NULL

您将NUM_STUFF声明为7(在C中应该是#define NUM_STUFF 7),并且您的STUFF数组有5个字符串,因为有两个逗号

"Silver Surfer likes surfing and space", /* you forgot the comma */
"GunSword likes mustard and exoskeletons", /* you forgot the comma */

(因为你忘记了逗号,下一个标记是一个字符串,两行都在一个字符串中连接)

我通过indent发现你的代码,然后将其编译为gcc -Wall -g u1.c -o u1并运行gdb u1来调试它。

您应该声明

#define NUM_STUFF 7
char *STUFF[NUM_STUFF] = {

实际上,您应该添加NULL终止数组的约定。 然后你会:

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",
  NULL
};

使用终止NULL进行编码可以更轻松地添加新句子。在这种情况下,无需定义或更改NUM_STUFF

然后你的主循环将是

for (i = 0; STUFF[i] != NULL; i++)

风格提示:我找到了

void find(int(*match)(char*));

很难读懂。我个人更喜欢声明typedef签名可能有针对性的功能,即我更喜欢编码

typedef int matchfun_t (char*);
void find (matchfun_t*);

答案 1 :(得分:2)

 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"

};

你忘了“,”因为数据不够