3

I came across the following code:

int H3I_hook(int (*progress_fn)(int*), int *id)
{
...
}

I don't understand the purpose of (int*) at the end of the first argument?

haccks
  • 104,019
  • 25
  • 176
  • 264
Zack
  • 1,205
  • 2
  • 14
  • 38
  • Do you understand it is a pointer to a function? – Eugene Sh. Aug 31 '16 at 17:46
  • @usr: Not even close. – Scott Hunter Aug 31 '16 at 17:48
  • 3
    @usr: If OP understood function pointers, we wouldn't be here. – Scott Hunter Aug 31 '16 at 17:51
  • There is a cool little program in K&R C book, around pg 137 [here](http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf) that parses complicated pointer declarations to english descriptions and back. Definitely worth a read – Rorschach Aug 31 '16 at 17:58
  • *I don't understand the purpose of `(int*)` at the end of the first argument* : There is a difference between "argument" and "parameter". What you are referring as argument is actually a parameter. – haccks Aug 31 '16 at 18:05
  • @jenesaisquoi: There's also an online version (no idea if it's based off of the K&R one or not, but it accomplishes the same thing): http://cdecl.ridiculousfish.com/ – Tim Čas Aug 31 '16 at 19:24
  • As others have mentioned you can use the program `cdecl` if you have it, or just use the website [to parse complicated C declarations and more.](http://cdecl.ridiculousfish.com/?q=int+%28*progress_fn%29%28int*%29) – smrdo_prdo Aug 31 '16 at 22:32

3 Answers3

10

Demystifying:

int (*progress_fn)(int*)

it can be interpreted like below:

int (*progress_fn)(int*)
 ^       ^          ^
 |       |          |___________ pointer to integer as argument
 |       |
 |     pointer to any function that has V and takes ^
 |
 |__________________________return type an integer
gsamaras
  • 71,951
  • 46
  • 188
  • 305
sjsam
  • 21,411
  • 5
  • 55
  • 102
2

int (*progress_fn)(int*) is function pointer decleration, and (int *) is the list of parameters the function accepts.

So, this:

int (*progress_fn)(int*)

is a pointer to a function that will return an int and will receive one parameter, of type int*.

So you have to understand that progess_fn is the actual parameter. All its relevant components define how the function's prototype is actually.


For more, read How do function pointers in C work?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
2

Given this declarartion:

int progress_callback(int* a);
//                    ^ this is the (int*) you asked about

You can call H3I_hook like this:

int id = something;
int x = H3I_hook(progress_callback, &id);
Elazar
  • 20,415
  • 4
  • 46
  • 67