Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to function returning function pointer

I would like to declare a variable of type pointer to function returning pointer to function. Essentially what the following does, but without any typedefs:

typedef void (*func)();
typedef func (*funky_func)();

funky_func ptr;

I tried the following

(void (*)()) (*ptr)();

but it gives an "undeclared identifier"-error for ptr (probably due to completely different parsing). Being not that well-versed in the intricacies of parsing C++, I'd like to know if this is even possible and if yes, how to do it.

(Please consider this an entirely artificial scenario for the sake of curiosity, without any practical reason. I am perfectly aware that in practice typedefs are the way to go here, if using function pointers at all.)

like image 239
Christian Rau Avatar asked Sep 16 '13 11:09

Christian Rau


People also ask

Can a function return a pointer value?

We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

Can we declare a function that can return a pointer to a function of the same type?

You cannot return a function in C - you return a pointer to a function. If you mean to define a function which returns a pointer to a function which again returns a pointer to a function and so on, then you can use typedef to implement it.

Can you return a pointer in C++?

Return Pointer from Functions in C++ Second point to remember is that, it is not good idea to return the address of a local variable to outside of the function, so you would have to define the local variable as static variable.

Which of the following declares a pointer to a function which returns a pointer to an integer?

int (*f) (int * ): A pointer to a function that takes an integer pointer as argument and returns an integer.


2 Answers

You can have a look at the declaration of signal() which is a function taking a void(*)() and returning one of those. The variable ptr can be declared like this:

void (*(*ptr)())()

The notation is a bit awkward and clearly inside out. It may be easier to use trailing return types:

auto (*ptr)() -> void (*)()

... or, of course, use trailing return types all the way through:

auto (*ptr)() -> auto (*)() -> void
like image 149
Dietmar Kühl Avatar answered Oct 15 '22 01:10

Dietmar Kühl


The general rule of C (and C++) declarations is: if you type the declaration as an expression, it will have the declaration's type.

So, you want a pointer to function which returns pointer to function returning void.

Let's say we have such a pointer, ptr. How to get void out of it?

  1. Dereference ptr, getting a function returning pointer to function returning void: *ptr

  2. Call the function, getting a pointer to function returning void: (*ptr)()

  3. Dereference that pointer, getting a function returning void: *(*ptr)()

  4. Call that function, getting void: (*(*ptr)())()

Now just turn this into a declaration:

void (*(*ptr)())();

P.S. I know other have answered in the meantime (and I've upvoted). But I wanted to show the generic process to arrive at the declaration form.

like image 25
Angew is no longer proud of SO Avatar answered Oct 15 '22 03:10

Angew is no longer proud of SO