关于C中字符比较的Segfault

cxz*_*xzp 2 c string char segmentation-fault

我有以下代码:

void click(int x, int y, zbar_window_t *window, ZBarGtk *self) {
    char* response = handle_click_events(x, y, window);
    printf("RESPONSE + %s\n", response);
    printf("%c\n", response[0]);
    if (response[0] == "0") {                                 //CRASHES HERE
        printf("inside \n");
        printf("%s\n", response++);
        g_signal_emit(self, self->enumACTIVE, 0, -1, response++);
    }
    else {
        g_signal_emit(self, self->enumACTIVE, 0, -1, response++);
    }
}
Run Code Online (Sandbox Code Playgroud)

它继续在规定的线上崩溃.

Don*_*ild 5

替换这个:

if(response[0] == "0")

有:

if(response[0] == '0')

"0"是一个字符串,'0'是一个字符.你不应该使用比较字符串==.

此外,你应该检查是否response是NULL该语句之后:

char* response = handle_click_events(x, y, window);
Run Code Online (Sandbox Code Playgroud)

否则printf("%c\n", response[0]);可能再次崩溃.