Daniel Moody
Daniel Moody

Reputation: 45

Functions as parameters in C

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

Upvotes: 1

Views: 111

Answers (4)

jim mcnamara
jim mcnamara

Reputation: 16379

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.

Upvotes: 1

Pascal Cuoq
Pascal Cuoq

Reputation: 80255

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.

Upvotes: 2

R Sahu
R Sahu

Reputation: 206557

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.

Upvotes: 1

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13171

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.

Upvotes: 2

Related Questions