Expert C Programming

(Jeff_L) #1

if (strcmp(f->fname, s) == SAME) {


return f;


}


}


/ file not found, so make a new one: /


f = allocate_file(s);


f->flink = file_hash_table[hash_value];


file_hash_table[hash_value] = f;


return f;


}


The effect was as though a hash table was not used; all the elements would be stored in a linked list
off element zero. This made it simple to debug, because you didn't have to calculate where anything
really should be. The ace programmer was able to quickly get the rest of the code working because he
did not have to worry about the interaction with hashing. When he was satisfied that the main routines
worked perfectly, he took some performance measurements, and decided to activate the hash function.
This was a two-line change in a single function. Here's the current version involving, as he put it,
"brain, pain, and gain".


int hash_filename (char *s)


{


int length = strlen(s);


return (length+4(s[0]+4s[length/2])) % FILE_HASH;


}


Sometimes taking the time to break a programming problem into smaller parts is the fastest way of
solving it.


Programming Challenge


Write a Hash Program


Type in the fragment of code above, and supply enough of the missing types, data, and code
to get it running as a program. Then (horrors!) debug it into existence.


How and Why to Cast


The term "cast" has been applied since the dawn of C to mean both "type conversion" and "type
disambiguation." If you say something like

Free download pdf