Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Typhoon

False Error

This topic is 5444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The compiler I''m using Dev C++ is giving an error about an undeclared variable even though I didn''t even use that name in the program. The error is [Warning] In function `main'': `keytab'' undeclared (first use in this function) (Each undeclared identifier is reported only once for each function it appears in.) Here''s the code.
/*main.c - counts C keywords*/

#include<stdio.h>
#include<string.h>
#include<ctype.h>

#define MAXWORD 100
#define NKEYS sizeof keytab / sizeof(struct key)

struct key{
    char *word;
    int count;
    } key_tab[] = {
        "auto", 0,
        "break", 0,
        "case", 0,
        "char", 0,
        "const", 0,
        "default", 0,
        "unsigned", 0,
        "void", 0,
        "volatile", 0,
        "while", 0
        };
    


int GetWord(char *, int);
int BinSearch(char *, struct key *, int);

int main(void)
{
    int n;
    char word[MAXWORD];
    
    while(GetWord(word, MAXWORD) != EOF)
        if(isalpha(word[0]))
                if((n = BinSearch(word, key_tab, NKEYS)) >= 0)
                        key_tab[n].count++;
    
    for(n = 0; n < NKEYS; n++)
        if(key_tab[n].count > 0)
                printf("%4d %s\n",                        
                        key_tab[n].count, key_tab[n].word);
    
    fflush(stdin);
    getchar();
        
    return 0;
    }
    
    /*BinSearch: find word in tab[0]...tab[n - 1]*/
    int BinSearch(char *word, struct key tab[], int n)
    {
        int cond;
        int low, mid, high;
        
        low = 0;
        high = n - 1;
        
        while(low <= high)
        {
                mid = (low + high) / 2;
                if((cond = strcmp(word, tab[mid].word)) < 0)
                      high = mid - 1;
                else if(cond > 0)
                      low = mid + 1;
                else
                      return mid;
        }
        
        return -1;
    }
    
    /*GetWord: get next word or character from input*/
    int GetWord(char *word, int lim)
    {
        int c;
        char *w = word;
        
        while(isspace(c = getch()))
                ;
        
        if(c != EOF)
            *w++ = c;
        if(!isalpha(c))
        {
           *w = ''\0'';           
           return c;
        }
        
        for(; --lim > 0; w++)
            if(!isalnum(*w = getch()))
            {
                ungetch(*w);
                break;
            }
        *w = ''\0'';
        return word[0];
    }
The code where it says the error is if((n = BinSearch(word, key_tab, NKEYS)) >= 0)

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Typhoon
The compiler I'm using Dev C++ is giving an error about an undeclared variable even though I didn't even use that name in
the program.


1) You HAVE indeed used the variable here:

#define NKEYS sizeof keytab / sizeof(struct key)

2) The way you have defined struct, I dont think it will compile. It SHOULD be:

struct key
{
char *word;
int count;
};

3)the array is key_tab[] or tab of type key? That got me confused. Of course it is of type struct key and hence it should be defined as:

key tab[] = {
"auto", 0,
"break", 0,
"case", 0,
"char", 0,
"const", 0,
"default", 0,
"unsigned", 0,
"void", 0,
"volatile", 0,
"while", 0
};

4)Lastly NKEYS should thus be defined as:
#define NKEYS sizeof(tab) / sizeof(struct key)




[edited by - hehenoobhehe on July 28, 2003 8:36:57 PM]
Since you use key_tab as the name of the array elsewhere in the program you should define your aray key_tab of type struct key as:
key key_tab[] = {..............};


[edited by - hehenoobhehe on July 28, 2003 8:40:49 PM]

Share this post


Link to post
Share on other sites
And once you fix that, please PLEASE put parentheses around the whole thing to save yourself the headache of order of operations later...

Share this post


Link to post
Share on other sites
Oh I didn''t see that. I checked through it already I guess I must have missed that, since the error was about 20 lines away.

Share this post


Link to post
Share on other sites
quote:
Original post by hehenoobhehe
. . . stuff . . .



That part of the code was fine. struct foo {} bar; would make bar an instance of struct foo.

You can also write struct {} bar;, in which case bar would be an instance of a nameless struct.

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
quote:
Original post by hehenoobhehe
. . . stuff . . .



That part of the code was fine. struct foo {} bar; would make bar an instance of struct foo.

You can also write struct {} bar;, in which case bar would be an instance of a nameless struct.

I get what you're saying but where did I (did I? ) make this point anywhere?

If you mean THIS:


struct key{
char *word;
int count;
}key_tab[] = {
"auto", 0,
"break", 0,
"case", 0,
"char", 0,
"const", 0,
"default", 0,
"unsigned", 0,
"void", 0,
"volatile", 0,
"while", 0
};



Then, I know what you're taking about, YES, its possible to do this as well BUT the point is that even here Typhoon seems to have made a mistake (simple typo), INSTEAD of typing key_tab EVERYWHERE, he has typed key (space) tab and hence I thought it was an obvious error.

[edited by - hehenoobhehe on July 29, 2003 4:56:30 AM]

Share this post


Link to post
Share on other sites
++reasons_why_defines_are_evil;
Remember this is a C and NOT C++ program!
what else do you think he should use instead of a macro to get this working?

?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!