Archived

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

False Error

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

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 on other sites
You do sizeof keytab

[edited by - cowsarenotevil on July 28, 2003 8:23:40 PM]

Share on other sites
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 on other sites
Of course you use keytab. It''s included through the macro.

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 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 on other sites
++reasons_why_defines_are_evil;

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 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 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 on other sites
C keywords: auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef typeid union unsigned using void volatile while

#include <stdio.h>int array[] = {1, 2, 3, 4, 5};const int array_size = sizeof array / sizeof *array;int main() {  printf("%d elements in array.", array_size); }

As for the other post, the only place I've seen "key tab" was in your post. I assumed you changed it because you though he meant
struct key {. . .};key tab[] = {. . .};
struct key {. . .} key_tab[] = {. . .};

And lastly, regarding correction #4, sizeof is an operator and doesn't need brackets unless used with a type name, which it wasn't.

So the only thing wrong with it was the missing underscore.

[edited by - smart_idiot on July 29, 2003 4:52:37 AM]

Share on other sites
quote:
Original post by smart_idiot

As for the other post, the only place I''ve seen "key tab" was in your post. I assumed you changed it because you though he meant
struct key {. . .};key tab[] = {. . .};
struct key {. . .} key_tab[] = {. . .};

[edited by - smart_idiot on July 29, 2003 4:48:22 AM]

well no, they were in his post in the #define statement which was the cause of the errors.(the others who have replied to this before/after me seems to agree that its indeed him who made a small typo)
and YES, I did mean what you just wrote! and thats why I did repost but the damn thing wont format properly!
Let me try editing again.

Share on other sites
I posted those assuming that he meant key (space) tab and hence assumed that the struct etc wasnt properly defined and so on.

so keeping that particular frame of reference in mind, I wasnt far away from the answer, was I?

(typhoon made some typos which made it rather confusing for me, pardon my small brain for this)

as for the const keyword: it totally slipped my mind! THANKS.