what do you mean by following line ?
void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*));
What you have essentially is a function pointer 'fnctn' which takes two function pointers for its two parameters. If we break this down bit by bit, what you have is the following:
The first parameter void(*)(int*, void**) is a function pointer returning void and taking an int* and void** as it's two parameters.
The second parameter int(*)(void**, int*) is a function pointer returning an int value and taking a void** and an int* as its two parameters.
Maybe it's clearer to see as follows:
typedef void(*param1)(int *, void**);
typedef int(*param2)(void**, int*);
typedef void(*fnctn)(param1, param2);
$ cdecl
Type `help' or `?' for help
cdecl> explain void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*));
declare fnctn as pointer to function (pointer to function (pointer to int, pointer to pointer to void) returning void, pointer to function (pointer to pointer to void, pointer to int) returning int) returning void
cdecl>
Wow, well, a typedef or two would be nice here, but it says...
declare a pointer to a function that returns void with the identifier "fnctn" that takes as parameters a function that returns void and takes an int* and a void** as parameters as well as a function that returns an int which takes a void** and an int* as parameters.
Further reading: Function pointer syntax
Try learning the clockwise spiral rule: http://c-faq.com/decl/spiral.anderson.html With this, you can learn what just about any function declaration will mean, thus enabling you to determine what it does.
Looks to me like declaration of a function pointer to a function that takes a function pointer to a function that takes int*, void** as arguments and returns void as the first parameter, and a function pointer to a function that takes void**, int* as arguments and returns int as the second parameter. This function is of return type void.
Clear as mud.
Related
As I starting to learn C++, this question may be stupid but I do not understand some strange pointers.
void (*p1(int*))(float*);
int* (*(*p2)(double(*fp1)(char), int*))(float *);
int* (*(*p3)(double(*)(char), int*))(double*);
I think p2 and p3 are function pointers pointing to a function that returns a pointer to int but I am totally lost about the rest.
Also I don't understand p1 at all.
Can anyone help me please?
Thanks.
Because of p1(int*) we know that p1 is a function taking argument int*. The return type of the function is the rest of the declaration once we delete the part we already analyzed, void (*)(float*) (i.e. pointer to function taking float * and returning void.
p2 and p3 have the same form, just p2 gives a name to the function parameter. So I will only address one of them, p3.
Because of (*p3)(double(*)(char), int *) we know that (*p3) is a function taking arguments double(*)(char) and int *. The return type of the function is the rest of the declaration without the part we already analyzed, i.e. int*(*)(double *). Since (*p3) has that function type, then p3's type is: pointer to that function.
p1 is a function that returns a pointer to a function. You are correct about p2. It is a pointer to a function that takes two arguments - a pointer to a function and a pointer to an int - and returns a pointer to a function. p3 is the same.
What does the following syntax mean?
set<element*, bool (*) (element *, element *)> * getNumbers();
I'm not familiar with the (*) part. Any explanation would be great. Thanks
Here it means that the second template parameter is a function pointer:
bool (*) (element *, element *)
is "pointer to a function that takes two element*s and returns bool".
You may also see (*) in connection with pointers to arrays;
int (*) [32]
is the type "pointer to an array of 32 ints".
It is a function pointer, more precisely bool (*) (element *, element *) is the type of a function pointer. In this case its a function that takes two element pointers and returns a bool.
Its makes more sense when you see it used as function parameter, then it will have a name after the first *. For example bool (*fun) (element *, element *).
It's a function pointer. You can read about it further here, for example:
http://www.cprogramming.com/tutorial/function-pointers.html
bool (*) (element *, element *) names the type of a pointer to function, where the function takes two element* pointers as parameters and returns a bool.
Second template argument is function pointer, designed to compare 2 element*. If you put such function pointer in constructor of std::set - you will be able to create set of elements with custom compare function (or without overloaded operator<).
What is void (*)() type in c++, how to cast a int to such type, I found it in a function like this:
function(const char*, long int, void (*)());
It's a function pointer: the address of a function with no parameters or return value.
You can't meaningfully cast an int to such a type. It's for passing a pointer to your own code for the function to call:
void my_callback() {/* do something */}
function("Hello", 42, my_callback);
Now the function can call its parameter as if it were a function, with the effect of calling your "callback" function.
In C/C++ the 'maximum munch' rule applies in evaluating expressions: from the starting point go left as much as possible to get a valid expression, then go right.
This is how a---b translates to (a--)-b and not a-(--b)
Back to your expression
void (*)()
(*) -> you have a pointer
(*)() -> you have pointer to a function what takes no parameters
void (*)() -> you have pointer to a function what takes no
parameters and does not return a value
So, your function is passed in a pointer to a function with no parameters, that returns nothing. Careful when passing in the pointer because it need to match exactly(return type and all).
Now, as far as to why you would want to do this, I can give you an practical example: the pthred_create function prototype:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
void *start_routine() is what the tread will run (like it's main) and that's why the create function needs this function pointer.
Some APIs use this method for more flexibility
I am so confused with this code in the book :
typedef int (*healthCalcFunc) (const GameCharacter&)
and I understand that
typedef double* PDouble, means the word PDouble can be used to declare a pointer to double.
But I can't figure out the meaning of typedef int (*healthCalcFunc) (const GameCharacter&)
Is there anyone can help me to explain this?
Thanks in advance
:)
typedef int (*healthCalcFunc) (const GameCharacter&);
It introduces a name called healthCalcFunc for a type which describes a function-pointer, taking one parameter of type const GameCharacter& and returning an int.
So this means, if you've a function as:
int some_function(const GameCharacter&)
{
//...
}
Then you can create a pointer-object which would point to the above function as:
healthCalcFunc pfun = some_function;
and then use pfun in place of some_function as:
some_function(args); /normal call
pfun(args); //calling using function pointer
//it is exactly same as the previous call
And benefit with this approach is that you can pass around pfun (or some_function) to other function as:
void other_function(healthCalcFunc pfun)
{
//..
pfun(args); //invoke the function!
//..
}
healthCalcFunc pfun = some_function;
other_function(some_function);
other_function(pfun);
Here other_function will use the function pointer to invoke the function. That way, next time you can pass another function matching the function signature to other_function and other_function will invoke that another function instead.
In cases like this, operator precedence tends to get in the way.
What this creates is an alias (named healthCalcFunc) for the type "pointer to a function taking a reference to a const GameCharacter as its parameter and returning an int".
int: return type
(*healthCalcFunc): Pointer to function -- must be in parens to bind the * to the name instead of the preceding int, which would declare a function returning a pointer instead of the desired pointer to a function.
(const GameCharacter &): the parameter list for the type of function this will point at.
It's a function pointer - hopefully your book will have explained these somewhere; if not, your favourite search engine should be able to point you in the right direction.
That is typedef for a function pointer for functions which return int and take const GameCharacter& as an argument.
You can create a function pointer using healthCalcFunc hp = &MyFunc; then use it as int n = (*hp)(GameCharacter());. Here MyFunc will have this signature: int MyFunc(const GameCharecter&);.
This is a function type definition. At first glance, it's strange, but you'll get used to it. basically, what it says is, define a type named healthCalcFunc, whose return value is an integer, and takes a constant GameCharacter reference as its only argument.
In general, the form of a function pointer declaration is as follows:
typedef return_type (*new_function_typename)(typeof_arg1, typeof_arg2, ...);
And you can use it like this:
new_function_typename functor;
functor = some_other_functions_name;
// or
functor = dlsym(dlopen_handle, "some_function_name"); // for dynamic loading
int retval = functor(arg1, arg2);
What is this syntax for in C++? Can someone point me to the technical term so I can see if I find anything in my text?
At first I thought it was a prototype but then the = and (*fn) threw me off...
Here is my example:
void (*fn) (int&,int&) = x;
It can be rewritten to
typedef void (*T) (int&, int&);
T fn = x;
The 2nd statement is obvious, which should have solved that = x; question. In the 1st statement, we make T as a synonym as the type void(*)(int&, int&), which means:
a pointer to a function ((*…))
returning void
and taking 2 arguments: int&, int&.
That is a function pointer to a function taking two int reference parameters, which returns nothing. The function pointer is called fn and is being assigned the value in x.
This declares and initializes a function pointer.
The name of the variable is fn, and it points to a function with the following signature:
void pointedToFunction(int&, int&)
The variable fn is initialized to the value contained in x.
The pointed-to function can be called using the following syntax:
int a;
int b;
(*fn)(a,b);
This is equivalent to
int a;
int b;
pointedToFunction(a,b);
Function pointer.
http://www.newty.de/fpt/intro.html#what
^ Okay source for a beginner. :-)
This is a pointer to a function that takes two references to ints and returns void.
That seems like a function pointer to a method that takes two integer references and does not return anything. The pointer will be named fn. You are assigning it to the address of x, which is hopefully a function that matches this description.
It's a function pointer to a function that takes 2 integers as arguments and returns void. x must be a function name.
It's a function pointer variable that gets initialized to the stuff to the right of =.
The function type could be written like this:
typedef void func_t(int&,int&);
The function pointer than would be:
typedef func_t *fn_t;
With these definitions the variable declaration would be much clearer:
fn_t fn = ...;
Wikipedia page with a few links on the subject: http://en.wikipedia.org/wiki/Function_pointer