I have found some code that I need to use for my application but there are two lines in it I can’t figure out what exactly do they do and how… Please, either explain them to me or direct me to a link so I can read more about it.
Dict* dcreate(hash_size size, hash_size (*hashfunc) (const char *));
Here I guess it is passing a function as a parameter with it’s parameter in the following bracket!?
hash_size i = dict->hashfunc(key) % dict->size;
and here, my guess is as good as my dog’s!
The hashfunc:
static hash_size def_hashfunc(const char* key){
hash_size s = 0;
while(*key){
s += (unsigned char) *key++;
}
return s;
}
Thanks.
For the first line, your guess is correct. That is the header for a function that accepts two arguments, one of which is of the
hash_sizetype, and another which is a pointer to a function whose argument is aconst char*and returns ahash_size.In the second line,
dictappears to be a pointer to a struct, sodict->hashfunc(key)calls the functionhashfunc, a pointer to which is stored in thedictstruct. The last part (... % dict->size) is just the modulo operation.