C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Compiler Error in example code for structures not understood Posted by jchavan on 7 Jan 2013 at 5:44 PM
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 <ctype.h>
#include <string.h>
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\n", 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 == '\t')
;

if(!isalpha(c)) {
if(c == '"') { /* double quotes */
while((c = getch()) != '"' && c != EOF && c != '\n')
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 != '\n')
;
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 != '\n')
if(c == '\\') {
if((c = getch()) == EOF)
return EOF;
}
if(c == EOF)
return EOF;
}
*w++ = c;
*w = '\0';
return c;
} else
*w++ = c;

while(--lim > 0) {
if(!isalpha(*w = getch())) { /* in case of EOF or " or / etc */
ungetch(*w);
break;
}
w++;
}
*w = '\0';
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;
}
Report
Re: Compiler Error in example code for structures not understood Posted by tolix on 9 Jan 2013 at 2:49 AM
Ok i compiled it in linux and it gives me a list of errors. Anyway add 2 little things and hopefully it will work on you. First of all add #include <stdio.h> for EOF. Then add typedef on struct key:

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

Done!


Report
Re: Compiler Error in example code for structures not understood Posted by jchavan on 9 Jan 2013 at 10:11 AM
It works!!
Thank you!
Report
Re: Compiler Error in example code for structures not understood Posted by jchavan on 9 Jan 2013 at 10:12 AM
It works!!
Thanks!!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.