Like Tree1Likes
  • 1 Post By c99tutorial

defn inside hash table

This is a discussion on defn inside hash table within the C Programming forums, part of the General Programming Boards category; Good afternoon. I'm studying the hash table program from K&R. I understand name is the content I want to find. ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question defn inside hash table

    Good afternoon. I'm studying the hash table program from K&R. I understand name is the content I want to find. But I don't know what is the role of defn in this program.

    Code:
     
    #include <stdio.h> 
    #include <string.h> 
    
    struct nlist { 
        
      struct nlist *next; /* próxima entrada na cadeia */ 
      char *name; /* nome definido */ 
      char *text;     /* texto substituído */
    
    };     
    
    #define HASHSIZE 101
    
    static struct nlist *hashtab[HASHSIZE];
    
    /* hash: create hash value for string s */
    
    unsigned hash(char *s) 
    { 
       unsigned hashval; 
       
       for(hashval = 0; *s != '\0'; s++) 
         hashval = *s + 31 * hashval;
       return hashval % HASHSIZE;      
    }
    
    /* lookup: look for s in hashtab */     
    struct nlist *lookup(char *s) 
    { 
      struct nlist *np;
      
      for(np = hashtab[hash(s)]; np != NULL; np = np->next)
        if(strcmp(s, np->name) == 0)
          return np; 
      return NULL;         
    }     
    
    struct nlist *lookup(char *); 
    
    /* install: put(name, defn) in hashtab */ 
    
    struct nlist *install(char *name, char *defn) 
    { 
       struct nlist *np; 
       unsigned hashval; 
       
       if( (np = lookup(name) ) == NULL) /* not found */ 
       { 
         np = (struct nlist *) malloc(sizeof(*np));  
         if(np == NULL || (np->name = strdup(name)) == NULL)
           return NULL;
         hashval = hash(name);
         np->next = hashtab[hashval];
         hashtab[hashval] = np;  
       }
         else                     
           free((void *) np->defn);
        if((np->defn = strdup(defn)) == NULL)       
          return NULL;
        return np;  
    }
    
    char *getword(char *w, int lim) 
    { 
          
    }          
    
    int main(void) 
    { 
            
      
       return 0;     
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    980
    I think the question is about of terminology:

    install(NAME, DEFN) : this will put the symbol with the name NAME into the table, and this name has a definition DEFN.

    example for the K&R implementation is a compiler that has the "#define" ability:

    #define A variable_a
    #define PI 3.14159
    #define FOO bar

    NAME refers to the macro name and DEFN refers to the replacement text. In this example you might do the following calls:
    Code:
    if (install("A", "variable_a") == NULL) error();
    if (install("PI", "3.14159") == NULL) error();
    if (install("FOO", "bar") == NULL) error();
    The function error() could be written to signal an error to the user (memory allocation error).

    In other terminology, the NAME part is sometimes called the "key" and the DEFN part is called the "value". So the two together form a single "key-value pair".
    Last edited by c99tutorial; 12-07-2012 at 12:13 PM.
    thames likes this.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    You must have typed it wrong. There is no member with that name, but there is an argument with that name.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Quote Originally Posted by c99tutorial View Post
    I think the question is about of terminology:

    In other terminology, the NAME part is sometimes called the "key" and the DEFN part is called the "value". So the two together form a single "key-value pair".
    Many Thanks. That was what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hash table
    By Aisthesis in forum C++ Programming
    Replies: 10
    Last Post: 09-22-2010, 08:58 AM
  2. Hash Table
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2009, 08:10 PM
  3. Hash Table
    By siavoshkc in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 04:29 PM
  4. What is a Hash Table?
    By rip1968 in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2002, 08:57 PM
  5. Hash table
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-14-2002, 06:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21