Next: strcpy() Up: Idiomatic C Previous: strcat()

Computing a hash value

unsigned hash(char *s)
/* Form hash value from string s */
   {
   unsigned hash_val;

   for (hash_val= 0;  *s != '\0';  s++)
	hash_val= *s + 31*hash_val;

   return hash_val % SIZE_OF_HASH_TABLE;
}

Note: use of pointer (not array index) to step through the string.

Note: use of for as a while, again.



Next: strcpy() Up: Idiomatic C Previous: strcat()


maspjw@
Tue Sep 27 15:29:34 BST 1994