Help->Can't Compile Ex in pp.140, K&R2 
Author Message
 Help->Can't Compile Ex in pp.140, K&R2

   When I tried the example in pp.140 in K&R2, the program *cannot* get
compiled:-
   (1) In Linux 1.2.8, using gcc 2.6.3, I got:
       "two or more data types in declaration of 'addtree'
   (2) In DOS 6.22, using Turbo C++ 2.01, I got:
Turbo C  Version 2.01  Copyright (c) 1987, 1988 Borland International
p140.c:
Error p140.c 17: Too many types in declaration
Error p140.c 22: Type mismatch in redeclaration of 'strdup'
Error p140.c 94: Type mismatch in redeclaration of 'strdup'
*** 3 errors in Compile ***

   I post up the essential part up on here to see if any one of you can
help solve this problem (the entire code has 140 lines). Thanks!  -cc

/*** *** K&R2 *** *** pp.140 *** ***/
        ...
struct tnode {           /* the tree node: */
   char *word;           /*  points to the text */
   int count;            /*  # of occurrences */
   struct tnode *left;   /*  left child */
   struct tnode *right;  /*  right child */

Quote:
};

        ...

struct tnode *addtree(struct tnode *, char *);
char *strdup(char *s);
        ...
        ...

/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
        ...

Quote:
}

/* make a duplicate of s */
char *strdup(char *s)
{
        ...
Quote:
}



Wed, 24 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2


Quote:
>   When I tried the example in pp.140 in K&R2, the program *cannot* get
>compiled:-
>char *strdup(char *s);

In my copy of K&R2, this line is

  char *strdup(char *);

without the 's'.




Wed, 24 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Quote:

>   When I tried the example in pp.140 in K&R2, the program *cannot* get
>compiled:-
>   (1) In Linux 1.2.8, using gcc 2.6.3, I got:
>       "two or more data types in declaration of 'addtree'
>   (2) In DOS 6.22, using Turbo C++ 2.01, I got:
>Turbo C  Version 2.01  Copyright (c) 1987, 1988 Borland International
>p140.c:
>Error p140.c 17: Too many types in declaration
>Error p140.c 22: Type mismatch in redeclaration of 'strdup'
>Error p140.c 94: Type mismatch in redeclaration of 'strdup'
>*** 3 errors in Compile ***
>   I post up the essential part up on here to see if any one of you can
>help solve this problem (the entire code has 140 lines). Thanks!  -cc

 Without an actual copy of the code, it is hard to tell. However,
 I'll guess you are missing a semi-colon on one of the lines before
 you define [not proto-type] the function strdup, or strdup() is
 defined already in a header file and your redefinition does not match.
 Solutions? If semi-colon is not problem, try renaming strdup.

 In fact, strdup is a common extension on Unix and Dos platforms, so
 this is probably the problem. Try calling it str_dup() and see if
 that clears things up. In otherwords, strdup is already taken,
 choose another name. I'm sure the ANSI zealots will chime in
 and say get a new compiler, but just renaming the function
 should do. I'm not familiar with TurboC++ but I believe clicking
 the right mouse button will bring up the help. Click it on strdup
 and see what it says or use the help, umm index? for strdup.



Wed, 24 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Quote:


>>   When I tried the example in pp.140 in K&R2, the program *cannot* get
>>compiled:-

>>char *strdup(char *s);

>In my copy of K&R2, this line is

>  char *strdup(char *);

>without the 's'.

Both forms are equivalent barring s being defined as a macro.

--
-----------------------------------------


-----------------------------------------



Thu, 25 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2


...

Quote:
> Without an actual copy of the code, it is hard to tell. However,
> I'll guess you are missing a semi-colon on one of the lines before
> you define [not proto-type] the function strdup, or strdup() is
> defined already in a header file and your redefinition does not match.
> Solutions? If semi-colon is not problem, try renaming strdup.

It may well be declared as:

char *strdup(const char *);

in string.h.

Quote:
> In fact, strdup is a common extension on Unix and Dos platforms, so
> this is probably the problem. Try calling it str_dup() and see if
> that clears things up.

Most likely.

Quote:
> In otherwords, strdup is already taken,
> choose another name. I'm sure the ANSI zealots will chime in
> and say get a new compiler, but just renaming the function
> should do.

strdup is a reserved identifier in string.h so the compiler is quite within
its rights to declare it in that header. It is one of my few criticisms of
K&R2 that it regularly misuses reserved identifiers.

--
-----------------------------------------


-----------------------------------------



Thu, 25 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Quote:

>       "two or more data types in declaration of 'addtree'
...
>    ...
>struct tnode *addtree(struct tnode *, char *);

That sort of error usually means you've done something like

struct foo {
  int bar;

Quote:
}   /* note the invisible semicolon */

struct tnode *addtree(struct tnode *, char *);

Quote:
>char *strdup(char *s);

Bad idea, strdup is in the implementation's namespace.  Anyway, the
parameter should be a char const *.

-zefram



Sun, 28 Feb 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

I (finally) got a copy of K&R2, but now I seem to be having vision (edition?) problems.  Where
on p.140 of K&R2 is this?  I don't see it...

Thanks,
MJB



Tue, 02 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

And, here was the messages the compiler gave out.
I used: gcc -Wall -ansi -pedantic -o p140 p140.c

p140.c:1: parse error before `is'
In file included from /usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include
                      /stdarg.h:36,
                 from /usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include
                      /stdio.h:2,
                 from p140.c:5:
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/va-alpha.h:16:
parse error before `}'
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/va-alpha.h:16:
ANSI C forbids data definition with no type or storage class
In file included from p140.c:5:
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/stdio.h:192:
parse error before `__gnuc_va_list'
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/stdio.h:193:
parse error before `__gnuc_va_list'
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/stdio.h:194:
parse error before `__gnuc_va_list'
p140.c:21: two or more data types in declaration of `addtree'
p140.c:26: conflicting types for `strdup'
/usr/local/lib/gcc-lib/alpha-dec-osf3.2/2.7.2.1/include/string.h:219:
previous declaration of `strdup'



Sun, 07 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

OK, here is the source code. Continue debugging, guys. :-)  -cc

/* p140.c on pp.140 K&R2 */

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

#define MAXWORD 100
#define BUFSIZE 100

struct tnode {                 /* the tree node: */
        char *word;                 /*  points to the text */
        int count;                  /*  number of occurrences */
        struct tnode *left;         /*  left child */
        struct tnode *right;        /*  right child */

Quote:
}

struct tnode *addtree(struct tnode *p, char *w);
struct tnode *talloc(void);

void treeprint(struct tnode *);
char buf[BUFSIZE];            /* buffer for ungetch */
char *strdup(char *s);
int bufp = 0;                 /* next free position in buf */
int getword(char *word, int lim);

/* word frequency count */
int main()
{
   struct tnode *root;
   char word[MAXWORD];

   root = NULL;
        while (getword(word, MAXWORD) != EOF) {
                if (isalpha(word[0])) {
                        root = addtree(root, word);
                        if (root == NULL) {
                                printf("Out of memory!\n");
                                return 1;
                        }
                }
        }
   treeprint(root);
   return 0;

Quote:
}

/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
   int cond;

   if (p == NULL) {   /* a new word has arrived */
                p = talloc();   /* make a new node */
                if (p == NULL)
                        return NULL;
                p->word = strdup(w);
                if (p->word == NULL) {
                        free(p);
                        return NULL;
                }
                p->count = 1;
                p->left = p->right = NULL;
        }
        else if ((cond = strcmp(w, p->word)) == 0)
                p->count++;     /* repeated word */
        else if (cond < 0) /* less than into left subtree */
                p->left = addtree(p->left, w);
                if (p->left == NULL)
                        return NULL;
        else               /* greater than into right subtree */
                p->right = addtree(p->right, w);
                if (p->right == NULL)
                        return NULL;
        return p;

Quote:
}

/* treeprint: in-order print of tree p */
void treeprint(struct tnode *p)
{
   if (p != NULL) {
      treeprint(p->left);
      printf("%4d %s\n", p->count, p->word);
      treeprint(p->right);
   }

Quote:
}

/* talloc: make a tnode */
struct tnode *talloc(void)
{
   return (struct tnode *) malloc(sizeof(struct tnode));

Quote:
}

/* make a duplicate of s */
char *strdup(char *s)
{
   char *p;

   p = (char *) malloc(strlen(s)+1);  /* +1 for '\0' */
   if (p != NULL)
      strcpy(p, s);
   return p;

Quote:
}

/* getword: get next word or character from input */
int getword(char *word, int lim)
{
   int c, getch(void);
   void ungetch(int);
   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];

Quote:
}

/* get a (possibly pushed backed) character */
int getch(void)
{
   return (bufp > 0) ? buf[--bufp] : getchar();

Quote:
}

/* push character back on input */
void ungetch(int c)
{
   if (bufp >= BUFSIZE)
      printf("ungetch: too many characters\n");
   else
      buf[bufp++] = c;
Quote:
}



Sun, 07 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Quote:

>OK, here is the source code. Continue debugging, guys. :-)  -cc

>/* p140.c on pp.140 K&R2 */

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

>#define MAXWORD 100
>#define BUFSIZE 100

>struct tnode {                 /* the tree node: */
>    char *word;                 /*  points to the text */
>    int count;                  /*  number of occurrences */
>    struct tnode *left;         /*  left child */
>    struct tnode *right;        /*  right child */
>};  /*was missing semicolon*/

/*error p140.c 51: To many types in declaration*/
/*after the addition of the semicolon, it compiled*/

- Show quoted text -

Quote:
>struct tnode *addtree(struct tnode *p, char *w);
>struct tnode *talloc(void);

>void treeprint(struct tnode *);
>char buf[BUFSIZE];            /* buffer for ungetch */
>char *strdup(char *s);
>int bufp = 0;                 /* next free position in buf */
>int getword(char *word, int lim);

>/* word frequency count */
>int main()
>{
>   struct tnode *root;
>   char word[MAXWORD];

>   root = NULL;
>    while (getword(word, MAXWORD) != EOF) {
>            if (isalpha(word[0])) {
>                    root = addtree(root, word);
>                    if (root == NULL) {
>                            printf("Out of memory!\n");
>                            return 1;
>                    }
>            }
>    }
>   treeprint(root);
>   return 0;
>}

>/* addtree: add a node with w, at or below p */
>struct tnode *addtree(struct tnode *p, char *w)
>{
>   int cond;

>   if (p == NULL) {   /* a new word has arrived */
>            p = talloc();   /* make a new node */
>            if (p == NULL)
>                    return NULL;
>            p->word = strdup(w);
>            if (p->word == NULL) {
>                    free(p);
>                    return NULL;
>            }
>            p->count = 1;
>            p->left = p->right = NULL;
>    }
>    else if ((cond = strcmp(w, p->word)) == 0)
>            p->count++;     /* repeated word */
>    else if (cond < 0) /* less than into left subtree */
>            p->left = addtree(p->left, w);
>            if (p->left == NULL)
>                    return NULL;
>    else               /* greater than into right subtree */
>            p->right = addtree(p->right, w);
>            if (p->right == NULL)
>                    return NULL;
>    return p;
>}

>/* treeprint: in-order print of tree p */
>void treeprint(struct tnode *p)
>{
>   if (p != NULL) {
>      treeprint(p->left);
>      printf("%4d %s\n", p->count, p->word);
>      treeprint(p->right);
>   }
>}

>/* talloc: make a tnode */
>struct tnode *talloc(void)
>{
>   return (struct tnode *) malloc(sizeof(struct tnode));
>}

>/* make a duplicate of s */
>char *strdup(char *s)
>{
>   char *p;

>   p = (char *) malloc(strlen(s)+1);  /* +1 for '\0' */
>   if (p != NULL)
>      strcpy(p, s);
>   return p;
>}

>/* getword: get next word or character from input */
>int getword(char *word, int lim)
>{
>   int c, getch(void);
>   void ungetch(int);
>   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];
>}

>/* get a (possibly pushed backed) character */
>int getch(void)
>{
>   return (bufp > 0) ? buf[--bufp] : getchar();
>}

>/* push character back on input */
>void ungetch(int c)
>{
>   if (bufp >= BUFSIZE)
>      printf("ungetch: too many characters\n");
>   else
>      buf[bufp++] = c;
>}

--
Craig

Manchester, NH
When you are friends with the phosphor people, you are never alone.
 -- Alice in Technoland "queen of the bitstream"


Mon, 08 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

: >struct tnode {                 /* the tree node: */
: >  char *word;                 /*  points to the text */
: >  int count;                  /*  number of occurrences */
: >  struct tnode *left;         /*  left child */
: >  struct tnode *right;        /*  right child */
: >};  /*was missing semicolon*/
:
: /*error p140.c 51: To many types in declaration*/
: /*after the addition of the semicolon, it compiled*/

Yes, it works now, thanks. But what is the "practical" use of this program
anyway? Or does it just illustrate how to deal with the tree thing?  -cc



Mon, 08 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Quote:


>: >struct tnode {                 /* the tree node: */
>: >      char *word;                 /*  points to the text */
>: >      int count;                  /*  number of occurrences */
>: >      struct tnode *left;         /*  left child */
>: >      struct tnode *right;        /*  right child */
>: >};  /*was missing semicolon*/
>:
>: /*error p140.c 51: To many types in declaration*/
>: /*after the addition of the semicolon, it compiled*/

>Yes, it works now, thanks. But what is the "practical" use of this program
>anyway? Or does it just illustrate how to deal with the tree thing?  -cc

The authors state on page xi that "For the most part, the examples are
complete, real programs, rather than isolated fragments". Also, "Besides
showing how to make effective use of the language, we have also tried
where possible to illustrate useful algorithms and principles of good
style and sound design".  The idea was to show how to keep a list of
words sorted at all times. A search engine for a database might search
pre-sorted data for remarkably fast search times.

--
Craig

Manchester, NH
When you are friends with the phosphor people, you are never alone.
 -- Alice in Technoland "queen of the bitstream"



Tue, 09 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

: .... The idea was to show how to keep a list of
: words sorted at all times. A search engine for a database might search
: pre-sorted data for remarkably fast search times.

OK. But just how to USE this one on pp.140.
When I ran the program, I input some words, then the program ended.
It seemed doing nothing.

Sorry, I'm just a newbie,
Clapton



Wed, 10 Mar 1999 03:00:00 GMT  
 Help->Can't Compile Ex in pp.140, K&R2

Reposting article removed by rogue canceller.

: .... The idea was to show how to keep a list of
: words sorted at all times. A search engine for a database might search
: pre-sorted data for remarkably fast search times.

OK. But just how to USE this one on pp.140.
When I ran the program, I input some words, then the program ended.
It seemed doing nothing.

Sorry, I'm just a newbie,
Clapton



Wed, 10 Mar 1999 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

2. Ex. 6-4 K&R2

3. Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

4. Help with compiling a "canned" program

5. ***&&&>>> HELP --- CXL --- HELP <<<&&&****

6. K&R2's implicit declaration of exit()

7. How to have my PP 2000 app work in PP 2002 emulator

8. help in K&R2

9. help with K&R2

10. compile a .pp file

11. K&R1 vs. K&R2

12. What does WinInet callback value <140> stand for?

 

 
Powered by phpBB® Forum Software