c中的分段错误strcmp

use*_*809 -2 c debugging gdb segmentation-fault strcmp

我正在尝试在 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;
}
Run Code Online (Sandbox Code Playgroud)

Noa*_*m M 5

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

int strcmp(const char *str1, const char *str2)
Run Code Online (Sandbox Code Playgroud)

你必须:

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

2) 确保st[i]&s具有空终止符 '\0'`。

3) 在调用之前检查st[i]&s是否指向内存中已分配的位置strcmp()