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; }



1Likes
LinkBack URL
About LinkBacks



