Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Compiler Error in example code for structures not understood

jchavanjchavan Member Posts: 3
I was attempting to solve a problem from K&R c programming exercise 6-1.

This code was posted as a solution however it has a compile time error at line 5 in the code below:(I am using Dev Cpp)

Can anyone please explain why this error is occurring?
_________________________________________________________
struct key {
char *word;
int count;
};
key keytab[] = {
"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 getword(char *word, int lim);
int binsearch(key keytab[], char *word, int nkeys);

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

#include
#include
int main(int argc, char *argv[])
{
char word[MAXWORD];
int index;

while(getword(word, MAXWORD) != EOF)
if(isalpha(word[0]))
if((index = binsearch(keytab, word, NKEYS)) >= 0)
keytab[index].count++;

int i;
for(i = 0; i < NKEYS; i++)
if(keytab[i].count > 0)
printf("%4d %s
", keytab[i].count, keytab[i].word);

getchar();
return 0;
}

int binsearch(key keytab[], char *word, int nkeys)
{
int low, high, mid;

int tmp;
low = 0;
high = nkeys - 1;
while(low <= high) {
mid = (low + high) / 2;
if((tmp = strcmp(word, keytab[mid].word)) < 0)
high = mid - 1;
else if(tmp > 0)
low = mid + 1;
else
return mid;
}
return -1;
}

int getword(char *word, int lim)
{
int c;
char *w = word;

int getch();
void ungetch(int c);

while((c = getch()) == ' ' || c == ' ')
;

if(!isalpha(c)) {
if(c == '"') { /* double quotes */
while((c = getch()) != '"' && c != EOF && c != '
')
if(c == '\') {
if((c = getch()) == EOF)
return EOF;
}
if(c == EOF)
return EOF;
} else if(c == '/') { /* comments */
if((c = getch()) == EOF)
return EOF;

if(c == '/') { /* // comments */
while((c = getch()) != EOF && c != '
')
;
return c;
} else if(c != '*') {
ungetch(c);
return '/';
}

while((c = getch()) != EOF) /* /* comments*/
if(c == '*') {
if((c = getch()) == EOF)
return EOF;
if(c == '/')
return c;
}
if(c == EOF)
return EOF;
} else if(c == '#') {
while((c = getch()) != EOF && c != '
')
if(c == '\') {
if((c = getch()) == EOF)
return EOF;
}
if(c == EOF)
return EOF;
}
*w++ = c;
*w = '';
return c;
} else
*w++ = c;

while(--lim > 0) {
if(!isalpha(*w = getch())) { /* in case of EOF or " or / etc */
ungetch(*w);
break;
}
w++;
}
*w = '';
return word[0];
}


int buffered = 0;
int buf = EOF;

int getch()
{
if(buffered) {
buffered = 0;
int temp = buf;
buf = EOF;
return temp;
}

return getchar();
}

void ungetch(int c)
{
buf = c;
buffered = 1;
}

Comments

Sign In or Register to comment.