简体   繁体   English

Segfault在C中的字符比较

[英]Segfault on char comparison in C

I have the following code: 我有以下代码:

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

It keeps on crashing on the stated line. 它继续在规定的行崩溃。

Replace this: 替换为:

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

with: 有:

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

"0" is a string, '0' is a character. "0"是字符串, '0'是字符。 You should not compare strings using == . 您不应该使用==比较字符串。

Also, you should check if response is NULL after this statement: 另外,您应在此语句后检查response是否为NULL

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

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

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

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