Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:31:27+00:00 2026-06-16T20:31:27+00:00

This here the code for the program which counts the number of occurrences of

  • 0

This here the code for the program which counts the number of occurrences of all the words in some input . Taken from the book K and R. I pretty much understood everything except why is the author using strdup() anyway.Why can’t we just assign(in the function addtree) p->word=w . In the structure tnode, word is clearly a pointer to a character and argument of addtree function is a character pointer.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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 */
};

struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
void treeprint(struct tnode *);
void ungetch(int);
int getword(char *, int);
int getch(void);

char buf[BUFSIZE];                    /* buffer for ungetch */
int bufp = 0;                         /* next free position in buf */


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

 root = NULL;
 while(getword(word, MAXWORD) != EOF)
  if(isalpha(word[0]))
    root = addtree(root, word);

 treeprint(root);
 exit(0);
}

                                      /* 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];
}

                                       /* 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 */
  p->word = strdup(w);
  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);
 else                                  /* greater than into right subtree */
  p->right = addtree(p->right, w);

 return p;
}

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

                                       /* 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);
 }
}

int getch(void) {
 return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) {
 if(bufp >= BUFSIZE)
  printf("ungetch: too many characters\n");
 else
  buf[bufp++] = c;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:31:28+00:00Added an answer on June 16, 2026 at 8:31 pm

    Because without strdup you just assign the address of the variable so both will point to the same memory (thus same data).

    char *ptr2 = ptr1;
    
    +----------+           +---------+
    |   PTR1   |---------->|  VALUE  |
    +----------+           +---------+
                                ^
    +----------+                |
    |   PTR2   |----------------+
    +----------+
    

    While with strdup a new block of memory is allocated and the characters are copied into new one:

    char *ptr2 = strdup(ptr1);
    
    +----------+           +---------+
    |   PTR1   |---------->|  VALUE  |
    +----------+           +---------+
    
    +----------+           +---------+    
    |   PTR2   |---------->|  VALUE  |
    +----------+           +---------+
    

    You see the difference?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's wrong with my code? This code is from my VB .NET program which
I have this code here printf '$request1 = select * from whatever where this
I have this code here, which is intended to allow any type of arguments:
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I basically have this program which fetches orders from a database. My problem is
Ok the error is showing up somewhere in this here code if($error==false) { $query
Editing this code here on Stackoverflow and I'm really near to get the result
so this code here dynamically adds buttons to my wpf windows application. I cant
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I found this code here class Usable; class Usable_lock { friend class Usable; private:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.