Do array of structure pointers point to NULL automatically/without being initialized? I would assume not, but it seems to be operating in that manner in the following line of code:
for (np = hashtab[hash(s)]; np != NULL; np = np->next)
np is a structure pointer, and s is a string.
None of the elements in hashtab[] have even been initialized when the loop begins. Every time I run the program the loop terminates immediately because hashtab[] contains NULL
The full code is below.
struct nlist { /* table entry: */
struct nlist *next; /* next entry in chain */
char *name; /* defined name */
char *defn; /* replacement text */
};
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; /* found */
return NULL; /* not found */
}
hashtabdefined?