1

I'm currently working on a project from intro C class, we are basically creating a hash table implementation in C but my current question pertains to how a certain function was written in code skeleton provided by my professor. Here is the header definition of the create method:

 Table* create(long (*hash)(void* key), bool (*equals)(void* key1, void* key2), void (*print)(void* key1, void* key2)); 

This appears to be pointers to functions as parameters? I'm not sure how to even call this, or what happens when it is called. I'm not even sure where these methods (hash, equals, and print) are coming from. Any help would be greatly appreciated. Thanks

1
  • When you have a pointer to a function or array, always use a typedef when you have the option. You may not in this case, but keep it in mind for next time you see chaos like this. Commented Apr 9, 2015 at 21:32

4 Answers 4

2

This appears to be pointers to functions as parameters?

Yes.

I'm not sure how to even call this

To invoke the function create, pass the addresses of some functions with the right types to call create:

create(&f1, &f2, &f3); 

or what happens when it is called.

Any place in the body of create where(*) the pointed function is invoked, the actual function (for instance f1) ends up being called with the provided arguments. It could be (*equals)(k1, k2); as a fictional example that could have occurred inside create.

(*) or, in this case, another function that will get the function pointers from the struct allocated by create where it will have stored them


In fact C allows you to write create(f1, f2, f3); in the first case and equals(k1, k2); in the second, but that's just a convenience.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thank you this makes a lot more sense. The syntax of this look so strange I didn't what to do with it, or even how to look it up
@DanielMoody Function pointers are strange in C, because they auto-convert in both directions. I don't know if I should tell you this, but look at example 4 in cs.berkeley.edu/~necula/cil/cil016.html . Anyway, just pick one style you are comfortable with between create(&f1…/ (*equals)(k1… and create(f1…/ equals(k1… and try to stick to it, but don't be surprised if you do not get compiler warnings when you forget a & or a * for a function or function pointer.
2

Yes, this is a function that takes three function pointers as arguments and returns a pointer to a Table. To use it, you'd have to define three functions that meet the criteria given:

long my_hash(void *key) { ... } bool my_equals(void *key1, void *key2) { ... } void my_print(void *key1, void *key2) { ... } 

and then call the function with them:

t = create(my_hash, my_equals, my_print); 

This looks like it's meant to create a hash table, and you have to give it a hash function and comparison function. The print function is probably just for debugging.

Comments

1

This appears to be pointers to functions as parameters?

Yes. That is correct.

I'm not sure how to even call this, or what happens when it is called.

You'll need to use functions that meet the signat ures of the parameters and call create using those functions. Example:

long myHashFunction(void* key) {...} bool myEqualsFunction(void* key1, void* key2) {...} void myPrintFunction(void* key1, void* key2)) {...} Table* table = create(myHashFunction, myEqualsFunction, myPrintFunction); 

What create does with those function depends can only be guessed. I have no idea what it does with them.

Comments

1

This should be a comment - too gabby to fit

Those are function pointers: long (*hash)(void* key), <- returns a long, uses a void * as input bool (*equals)(void* key1, void* key2), <- return 0 or 1 (True/False) void (*print)(void* key1, void* key2)); <- no return 

Since those are pointers, the actual function names are names you create (or the prof may have created them for you with any name, including hash, equals, and print).

But "hash" returns an offset into to a hash table (maybe an array). "equals" tests whether two input values are the same hash - sameness may be purely subjective. Ask your prof. print displays a hash entry, meaning I suppose, it finds the entry and prints the information in the hashed array or object for the key value. Look up 'associative array' to see what I mean.

1 Comment

Comments shouldn't normally be posted as answers, but this isn't all that bad as an answer, so I haven't flagged or downvoted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.