我正在为类开发一个简单的 lex 程序,我在其中创建了一个非常基本的符号表,只是一个字符串数组,带有用于搜索的线性扫描。我已将其声明为:
char* identifiers[100];
我是这样使用它的:
found = false;
for (i = 0; i < seen_identifiers; i++) {
if (!strcmp(identifiers[i], yytext)) {
printf("Identifier \"%s\" already in symbol table", yytext);
found = true;
break;
}
}
if (!found) {
printf("identifier: %s\n", yytext);
seen_identifiers++;
identifiers[seen_identifiers] = yytext;
}
但是我总是在 strcmp 调用中遇到段错误。我确定我搞砸了一些非常简单的事情。
最佳答案
seen_identifiers++;
identifiers[seen_identifiers] = yytext;
如果 seen_identifiers
从 0 开始,您永远不会分配给 identifiers[0]
,因此 strcmp
会出错。
关于c - 如何声明 C 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1551492/