Doing an assignment to create a struct that holds keywords and then is used with a binary search to counter the total keywords in a program. Below is my code and below it is the errors i get when I attempt to compile it. First question is what is wrong with my struct setup that is causing the division by 0 error.
#include <stdio.h>
#define MAXWORD 20
#define LETTER 'a'
#define DIGIT '0'
#define NKEYS (sizeof (keytab)/ sizeof (struct key))
/*whole bytes bytes in element*/
struct keytab
{
char *keyword;
int keycount;
}
keytab[NKEYS]={
"auto", 0,
"break", 0,
"case",0,
"char", 0,
"const", 0,
"continue", 0,
"default", 0,
"do", 0,
"double", 0,
"else", 0,
"enum", 0,
"extern", 0,
"float", 0,
"for", 0,
"goto", 0,
"if", 0,
"int", 0,
"long", 0,
"register", 0,
"return", 0,
"short", 0,
"signed", 0,
"sizeof", 0,
"static", 0,
"struct", 0,
"switch", 0,
"typedef", 0,
"union", 0,
"unsigned", 0,
"void", 0,
"volatile", 0,
"while", 0,
};
int binsearch(char word,struct key tab,int n);
int getword(char w,int lim);
int main() /* count C keywords */
{
int n,t;
char word[MAXWORD];
while ((t = getword(word, MAXWORD)) !=EOF)
if (t== LETTER)
if (n = binsearch(word, keytab, NKEYS)) >=0)
keytab[n].keycount++;
for (n=0; n < NKEYS; n++)
if (keytab[n].keycount >0)
printf("%4d %s\n", keytab[n].keycount, keytab[n].keyword);
}
int binsearch(char *word,struct key tab[],int n) /* find word in tab[0]....tab[n-1]*/
{
int low, high, mid, cond;
low=0;
high = n-1;
while (low <= high)
{
mid = (low+high)/2;
if (( cond =strcmp(word, tab[mid].keyword)) < 0)
high = mid -1;
else if (cond >0)
low = mid +1;
else
return(mid);
}
return (-1)
}
int getword(char *w,int lim) /* get next word from input */
{
int c, t;
if (type(c = *w++ = getch()) != LETTER)
{
*w = '\0';
return(c);
}
while (-- lim > 0)
{
t = type (c = *w++ = getch());
if (t != LETTER && t != DIGIT)
{
ungetch(c);
break;
}
}
*(w-1) = '\0';
return(LETTER);
}
type(c) /* return type of ASCII character */
int c;
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
return(LETTER);
else if (c >= '0' && c <= '9')
return(DIGIT);
else
return(c);
}
"keyword.c", line 13: undefined symbol: keytab
"keyword.c", line 13: incomplete struct/union/enum key: sizeof()
"keyword.c", line 13: division by 0
"keyword.c", line 13: zero or negative subscript
"keyword.c", line 14: too many array initializers
"keyword.c", line 56: warning: improper pointer/integer combination: arg #1
"keyword.c", line 58: warning: improper pointer/integer combination: arg #1
"keyword.c", line 58: argument #2 is incompatible with prototype:
prototype: struct key {} : "keyword.c", line 48
argument : int
"keyword.c", line 58: warning: division by 0
"keyword.c", line 58: syntax error before or at: >=
"keyword.c", line 61: warning: division by 0
"keyword.c", line 62: cannot dereference non-pointer type
"keyword.c", line 62: warning: left operand of "." must be struct/union object
"keyword.c", line 62: cannot access member of non-struct/union object
"keyword.c", line 63: cannot dereference non-pointer type
"keyword.c", line 63: warning: left operand of "." must be struct/union object
"keyword.c", line 63: cannot access member of non-struct/union object
"keyword.c", line 63: cannot dereference non-pointer type
"keyword.c", line 63: warning: left operand of "." must be struct/union object
"keyword.c", line 64: cannot recover from previous errors
cc: acomp failed for keyword.c