有常数:
const uint8_t background_part[5] = "\" & \"";
const uint8_t and_part[6] = "\" && \"";
const uint8_t output_to_start_part[5] = "\" > \"";
const uint8_t output_to_end_part[6] = "\" >> \"";
const uint8_t input_from_part[5] = "\" < \"";
const uint8_t bracket_left_part[5] = "\" ( \"";
const uint8_t bracket_right_part[5] = "\" ) \"";
另一个模块中有一个函数:
int32_t comp_last_copa_part (const copa *t, const uint8_t *buf, int32_t ssize_buf);
当您将 background_part 常量作为 buf 参数传递给它时,例如,在 gdb 调试器中,会发生以下情况:
comp_last_copa_part (t=0x55555555b2e0, buf=0x555555557093 <background_part> "\" & \"\" && \"\" > \"\" >> \"\" < \"\" ( \"\" ) \"Set termios new attributes", size_buf=5)
问题:为什么要传递这样一个字符串?什么是 buf=0x555555557093 <background_part> "" & "" && "" > "" >> "" < "" ( "" ) "设置 termios 新属性"
“Set termios”通常是 perror 中的一行,这是一个代码片段,它位于常量声明的下方:
} else {
perror("Set termios new attributes");
exit(1);
}
这个问题我不知道怎么命名,如果版主知道,请指正。
当然,我从代码中可以看出为什么会这样,比如这是下面所有行的开头,但是在我看来这是不正确的,这样的问题是如何解决的呢?
正如 ori 所写,数组的大小不适合字符 '\0' - 字符串的结尾。并且在某些编译器中会发出警告 -
initializer-string for array of chars is too long“初始化字符串的大小太大”。只需使所有数组比字符串中的字符多 1 即可。