cxzp
cxzp

Reputation: 672

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.

Upvotes: 2

Views: 1360

Answers (1)

0xF1
0xF1

Reputation: 6116

Replace this:

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

with:

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

"0" is a string, '0' is a character. You should not compare strings using ==.

Also, you should check if response is NULL after this statement:

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

otherwise printf("%c\n", response[0]); may crash again.

Upvotes: 5

Related Questions