[英]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.