开发者

How to declare C array of strings

开发者 https://www.devze.com 2022-12-08 01:54 出处:网络
I\'m working on a simple lex program for class, and in it I\'m creating a very rudimentary symbol table, just an array of strings with a linear scan for search.I\'ve declared it as:

I'm working on a simple lex program for class, and in it I'm creating a very rudimentary symbol table, just an array of strings with a linear scan for search. I've declared it as:

char* identifiers[100];

And I'm using it like so:

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;
}

However I consistently get a segfault at the strcmp call. 开发者_如何学JAVAI'm sure I've screwed up something super simple.


seen_identifiers++;
identifiers[seen_identifiers] = yytext;

If seen_identifiers starts at 0, you never assign to identifiers[0] and so the strcmp will fault.

0

精彩评论

暂无评论...
验证码 换一张
取 消