C pass pointer as argument in function

时间:2016-08-31 17:45:19

标签: c function pointers parameters

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?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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?

答案 2 :(得分: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);