【问题标题】:How to declare C array of strings如何声明 C 字符串数组
【发布时间】:2009-10-11 19:14:12
【问题描述】:

我正在为类开发一个简单的 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 调用中始终遇到段错误。我确定我搞砸了一些超级简单的事情。

【问题讨论】:

  • 如何将字符串分配给数组?您也可以发布该示例代码吗?

标签: c arrays string lex


【解决方案1】:
seen_identifiers++;
identifiers[seen_identifiers] = yytext;

如果 seen_identifiers 从 0 开始,您永远不会分配给 identifiers[0],因此 strcmp 会出错。

【讨论】:

  • ...我觉得自己像个白痴,这就是我整天用高级语言写作的收获。
  • 不错的观察。我在考虑空终止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2010-09-07
  • 2012-10-22
  • 2018-02-16
  • 2011-04-18
相关资源
最近更新 更多