0

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. I'm sure I've screwed up something super simple.

1
  • how are you assigning the strings to the array? Can you post that sample code as well? Commented Oct 11, 2009 at 19:20

1 Answer 1

4
seen_identifiers++;
identifiers[seen_identifiers] = yytext;

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

Sign up to request clarification or add additional context in comments.

2 Comments

... I feel like an idiot, this is what I get for writing in high level languages all day.
Nice observation. I was thinking of null termination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.