为什么我在运行此功能时会出现分段故障?

-4 c flags getopt segmentation-fault

当我发送标志-h或-H时,为什么会出现分段错误.

bool parse_command(int argc, char **argv, bool *header, char **fileName)
{

    if (!argv)
        return false;

    bool *flagh = false;
    bool *flagH = false;

    char *options = "Hh";
    int opt = 0;

    while ( (opt = getopt(argc, argv, options ) ) != -1)
    {
        printf("HELLO");
        switch (opt)
        {
            case 'h': 
                *flagh = true; 
                break;
            case 'H':
                *flagH = true; 
                break;
            default:
                usage_p1();  
                return false;
        }

    }
    printf("%d", opt);
    // Implement this function
    return true;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*mit 6

这两行是你的问题:

bool *flagh = false;
bool *flagH = false;
Run Code Online (Sandbox Code Playgroud)

你声明flagh,并flagH指针,以布尔值,但他们不随地点呢.事实上,他们最终无处可指,因为你的初始化等同于

bool *flagh = NULL;
bool *flagH = NULL;
Run Code Online (Sandbox Code Playgroud)

你可能不希望这些成为指针.将声明更改为

bool flagh = false;
bool flagH = false;
Run Code Online (Sandbox Code Playgroud)

将作业更改为

flagh = true; 

flagH = true; 
Run Code Online (Sandbox Code Playgroud)