繁体   English   中英

Segfault在C中的字符比较

[英]Segfault on char comparison in C

我有以下代码:

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++);
    }
}

它继续在规定的行崩溃。

替换为:

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

有:

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

"0"是字符串, '0'是字符。 您不应该使用==比较字符串。

另外,您应在此语句后检查response是否为NULL

char* response = handle_click_events(x, y, window);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM