Reputation: 1031
Can you please explain what does the following line means?
typedef int (*Callback)(void * const param,int s)
Upvotes: 3
Views: 1708
Reputation: 76755
It means that Callback
is a new name for the type : pointer to a function returning an int and taking two parameters of type 'const pointer to void' and 'int'.
Given a function f
:
int f(void * const param, int s)
{
/* ... */
}
The Callback
can be used to store a pointer to f
:
Callback c = &f;
The function f
can be later invoked through the pointer without directly referring to its name :
int result = c(NULL, 0);
At the point of the call, the name f
does not appear.
Upvotes: 6
Reputation: 264381
It declares a function type:
// Set up Callback as a type that represents a function pointer
typedef int (*Callback)(void * const param,int s);
// A function that matches the Callback type
int myFunction(void* const param,int s)
{
// STUFF
return 1;
}
int main()
{
// declare a variable and assign to it.
Callback funcPtr = &myFunction;
}
Upvotes: 2
Reputation: 106096
It creates a new "alias" or name by which you can refer to pointers to functions that return int
and take two parameters: a void* const
and an int. You can then create variables of that type, assign to them, invoke the function through them etc as in:
int fn(void * const param,int s) { ... }
Callback p;
p = fn;
int x = p(NULL, 38);
Note that typedef
s do not really create new types... every equivalent typedef is resolved to the single real type for the purposes of overload resolution, template instantiation etc..
Upvotes: 2