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
Related
While doing my homework i decided to use function pointer as a callback function. I am not going to post the whole homework here as it is too large. Here i wrote small test code to illustrate the error i am getting.
#include <iostream>
void function()
{
std::cout << "test";
}
int main()
{
void* functionPtr(int) = function;
functionPtr();
}
I am getting error C2059: syntax error: '=' here void* functionPtr(int) = function;. If i hower my mouse over = sign it says next: "function functionPtr cannot be initialized". If i change that to ...
void* functionPtr();
functionPtr = function;
...i will be getting error C2659: '=': function as left operand.
Am i using function pointers wrong way?
I use Visual Studio 2019 btw.
Thank you for any help.
This isn't the right syntax. You declare a function, not a function pointer.
Here's how a function pointer is declared:
void(*functionPtr)(int);
As you noticed, the star is beside the name. If there is no parenthesis, the star will apply to the return type and not the name.
Or with a trailing return type:
auto(*functionPtr)(int) -> void;
Also, your function called function don't take an int as parameter. So it should be like this:
void(*functionPtr)() = function;
You got the types wrong. There is no int parameter for in function and you missed a *. Your declare functionPtr as a function. For a function pointer you need:
void(*functionPtr)() = function;
// ^--- "pointer to function"
However, since C++11, using function pointers got much simpler, as you can do
using functionPtrType = void(*)();
functionPtrType p = function;
Or
using functionPtrType = decltype(&function);
functionPtrType p = function;
which has a tiny disadvantage of not making it obvious that a function pointer is involved. The advantage is that it wont break when you change the signature of function.
On the other hand, once you use decltype (and you do not need the type of the function pointer elsewhere) you can as well shorten it to:
decltype(&function) functionPtr = function;
Eventually, this is already avoiding so much to spell out the type that you can simply use auto:
auto functionPtr = &function;
To create a pointer to a function, you need to use parentheses to override the normal binding of the *. void *f() means f is a function returning a pointer to void. To instead get a pointer to a function returning void, you need void (*f)();
So in your case, the code would look something like void (*functionPtr)() = function;
This is a very novice question, but I have noticed that some C-functions are of the form:
int* foo(int *N){...}
Can someone:
a) explain what the int* foo means? In one such function there is no
return statement.
b) explain how flexible the int *N is? I.e., I know that this means that the argument is a pointer to an int, but from what I understand from looking at one such function, this means that the function foo can actually take vector arguments. This makes no sense to me.
a) explain what the int* foo means? In one such function there is no return statement.
It means foo is a function that returns a pointer to an int. If such a function does not have a return statement, then calling that function will lead to undefined behavior.
From the C++ Standard:
6.6.3 The return statement
...
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
b) explain how flexible the int *N is?
That depends on the implementation of foo. The following are syntactically valid ways to call foo. Whethe they are semantically valid depends on foo.
int a;
int b[10];
int* c = new int;
int* d = new int[20];
foo(&a);
foo(b);
foo(c);
foo(d);
foo(nullptr);
In this example, foo is a pointer to a function taking one argument, an integer pointer, and that returns int. It's as if you're declaring a function called "*foo", which takes an integer pointer and returns an interger; now, if *foo is a function, then foo must be a pointer to a function.
The key to writing the declaration for a function pointer is that you're just writing out the declaration of a function but with (*func_name) where you'd normally just put func_name.
int* foo(int *N) is a function that takes a pointer, this pointer points to at least one int, but possibly to an array of integers, C is a bit sloppy on this disiction.
{
int n = 0;
foo(&n);
}
or maybe
{
int a[17];
foo(a);
}
What of the below is wrong please?
It is my understanding that a pointer represents an address of something of some type.
So, int i = 18, a pointer to it is int *pI = &i;
The following 2 declarations are valid
void foo (int &something) // Will accept an address of something
void bar (int *something) // Will accept a pointer to something
When we declare a function as
void bar (int *something)
We better send a pointer to something. Indeed, foo(pI) works.
Following the same logic, when looking at
void foo (int &something)
We should send it an address of something pointing to an int as an argument, so then:
Why is foo(&i) wrong?
void foo (int &something) // Will accept an address of something
This is incorrect: int& is a reference, a concept that is similar to pointers in certain ways, but not at all identical.
References are similar to pointers in that a value can be changed through a reference, just like it can be changed through a pointer. However, there is no such thing as "null reference", while NULL pointers are very common.
When you call a function that takes a reference, you simply pass the variable the reference to which you are taking - no & operator is required:
void foo (something); // "something" must be a variable
The declaration void foo (int &something) takes a reference variable, not a pointer.
You call it exactly the same as you would call void foo (int something), except that something is passed by reference, not by value.
void foo (int &something) // Will accept an address of something
No, though I can understand the confusion. That function takes a reference to int. References are guaranteed to be non-null in a well defined program. It is similar conceptually to a pointer, but semantically different.
To call that function you don't need to do anything special.
int i = 10;
foo(i); // passed to foo by reference
Because in C++ the & means "reference type" in the context of a function prototype as you are using it here. Your function:
void foo (int &something)
is actually specifying that an integer reference type should be passed, so you would simply call this function like this:
foo(i);
Note that in this case you are still passing the address of i into the function, but you are doing it as a reference type rather than a raw pointer.
There are two different meanings for &.
The first is to create a pointer from an existing variable.
The second is to make a reference.
Even though they both use the same character, they are two completely different things.
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 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.