c语言中的分段故障strcmp

c语言中的分段故障strcmp,c,debugging,segmentation-fault,gdb,strcmp,C,Debugging,Segmentation Fault,Gdb,Strcmp,我试图用c语言运行一个程序,从用户那里接收文本文件和字符串,然后在文件中搜索该字符串。它不断地得到一个分段错误,gdb将我指向这个函数,但我不确定问题是什么。我很确定这与strcmp的调用有关,但我不确定。在此问题上的任何帮助都将不胜感激 int inTable( const char *s ) { int i; for( i=0; i<numLines; ++i ) { if( strcmp( st[i], s ) == 0 )

我试图用c语言运行一个程序,从用户那里接收文本文件和字符串,然后在文件中搜索该字符串。它不断地得到一个分段错误,gdb将我指向这个函数,但我不确定问题是什么。我很确定这与strcmp的调用有关,但我不确定。在此问题上的任何帮助都将不胜感激

int inTable( const char *s )
{
    int i;

    for( i=0; i<numLines; ++i )
    {
        if( strcmp( st[i], s ) == 0 )
                return 1;
    }

    return 0;
}
int inTable(常量字符*s)
{
int i;

对于(i=0;i我认为您应该检查s:

int inTable(const char *s) 
{
     if (s == NULL)
     {
          return 0;
     }

     // invoke strcmp
}

确保const char*s以'\0

结尾。您应该检查是否正确使用了strcmp()
,API是:

int strcmp(const char *str1, const char *str2)
你必须:

1) 验证
st[i]
,您的第一个参数是指针

2) 确保
st[i]
s
具有空终止符“\0”


3) 在调用
strcmp()

st是使用fgets()@user4959809读入的文件之前,检查
st[i]
s
是否指向内存中的分配位置?