我有一个看起来像这样的函数:
int lexWhitespace(TokenizerOutput* input) {
printf("he");
if (!(14 > input->toStillParse[0] > 8) && !(input->toStillParse[0] == 32)) {
// checks if the first character in the toStillParse section of input is whitespace.
return -1;
} else {input->tokenizerOutput[0] = input->toStillParse[0];}
for (int i = 1; ; i++) {
if ((14 > input->toStillParse[0] > 8) || (input->toStillParse[0] == 32)) {
// checks if the first character in the toStillParse section of input is whitespace.
input->tokenizerOutput[i] = input->toStillParse[i];
} else {return 0;}
}
}
接受这个结构:
struct TokenizerOutput {
const char* toStillParse; // holds the text that still needs to be parsed.
char* tokenizerOutput; // holds the text that was just output by tokenizer function.
};
typedef struct TokenizerOutput TokenizerOutput;
当我尝试在这样的主函数中调用它时:
int main(void) {
printf("hello\n");
TokenizerOutput j = {" k", " "};
printf("%s\n", (&j)->toStillParse);
lexWhitespace(&j);
return 0;
}
我得到一个段错误。段错误发生在函数 lexWhitespace 甚至运行任何东西之前,因为它不打印“he”。我不知道为什么会这样。任何帮助将不胜感激。我正在使用 gcc 9.3.0。