A function that returns a pointer on a function

B

Boris Sargos

Hi,

I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?

Thanks.
Boris
 
J

John Harrison

Boris Sargos said:
Hi,

I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?

Thanks.
Boris

Trying to do that without using a typedef is a real test of your
understanding of type declarations. The easy way is with a typedef

typedef void (*FUNC_PTR)(void); // or whatever

FUNC_PTR some_function()
{
...
}

It is however impossible to define a function that returns a pointer to
itself, that would mean an infinite recursion in the type of that function.

john
 
S

Siemel Naran

Trying to do that without using a typedef is a real test of your
understanding of type declarations. The easy way is with a typedef

Without typedef I think this is it:

void (*some_function())();

This is a function some_function taking no arguments and returning a pointer
to a function taking no arguments and returning a void. It's similar to a
function taking a reference to an array of elements. But of course, what
you have below is cleaner:
typedef void (*FUNC_PTR)(void); // or whatever

FUNC_PTR some_function()
{
...
}

It is however impossible to define a function that returns a pointer to
itself, that would mean an infinite recursion in the type of that
function.

This makes sense, because of

typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever

But what about

void (*)(*some_function())();

Just guessing. Could be wrong.
 
J

John Harrison

Siemel Naran said:
Without typedef I think this is it:

void (*some_function())();

That's right.
This is a function some_function taking no arguments and returning a pointer
to a function taking no arguments and returning a void. It's similar to a
function taking a reference to an array of elements. But of course, what
you have below is cleaner:

function.

This makes sense, because of

typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever

But what about

void (*)(*some_function())();

Just guessing. Could be wrong.

Syntax error on my compiler. If it's not possible with a typedef I don't see
why you think it might be possible without a typedef.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top