returning function pointer

R

ranjmis

Hi all,

I have come across a piece of code wherein a function returns a
function pointer as it seems to me but not very clear from the
prototype.

As shown below - although return type is void - (*master())() returns a
function pointer some_func & meanwhile calls that function and prints
the message "hi from some_func"

please help in interpreting this definition of "master" "function"

-------------------
#include<stdio.h>

void some_func()
{
printf("hi from some_func");
}

void (*master())(int)
{
return some_func;
}

int main()
{
(*master())(7);
return 0;
}
---------------------
 
B

Ben C

Hi all,

I have come across a piece of code wherein a function returns a
function pointer as it seems to me but not very clear from the
prototype.

As shown below - although return type is void - (*master())() returns a
function pointer some_func & meanwhile calls that function and prints
the message "hi from some_func"

please help in interpreting this definition of "master" "function"

-------------------
#include<stdio.h>

void some_func()
{
printf("hi from some_func");
}

void (*master())(int)
{
return some_func;
}

int main()
{
(*master())(7);
return 0;
}
---------------------

Function definitions look like the types they return.

A more ordinary-looking function:

int f(int) { /* body */ ... }

Take away the parameters and the body and you have:

int f

And this makes sense because the value of f is of type int. I mean
"value" here in something like the mathematical sense in which functions
are said to have values.

So a function that returns a pointer to a function is just the same:

void (*master)(int)

is the type of a pointer to function that takes an int and returns void.

Put the param list and body back:

void (*master())(int) { return some_func; }

and you have it-- the definition of a function that takes unspecified
parameters and returns a pointer to a function that takes an int and
returns void. Note there are two parameter lists here. "()" are the
parameters to master itself. "(int)" is the parameter list for the
function pointer that master returns. Very confusing.

You are passing 7 to "some_func", which has an unspecified parameter
list. I suspect this may be "undefined behaviour". Hopefully one of the
more expert readers of this NG will confirm this.

The whole thing is made much easier to read with a typedef:

#include<stdio.h>

void some_func()
{
printf("hi from some_func");
}

typedef void (*fn_t)();

fn_t master()
{
return some_func;
}

int main()
{
fn_t fn = master();
fn(7);
return 0;
}
 
P

Pedro Graca

ranjmis said:
I have come across a piece of code wherein a function returns a
function pointer as it seems to me but not very clear from the
prototype.

As shown below - although return type is void - (*master())() returns a
function pointer some_func & meanwhile calls that function and prints
the message "hi from some_func"

Nope, the return type of master is not void. See below.

Try changing some_func() to different things and
change master() definition to account for the changes

int some_func(void);
void some_func(int);
int some_func(int);
...

When you change some_func() to return something, don't forget to add a
return statement to the function body.

please help in interpreting this definition of "master" "function"

see faq 1.21 and 1.22
<http://www.c-faq.com/decl/cdecl1.html>
<http://www.c-faq.com/decl/recurfuncp.html>

[code snipped]
void (*master())(int)

master master
master() is a function without accepting
unspecified parameters
*master() that returns a pointer
(*master()) to a function
(*master())(int) that accepts one `int` parameter
void (*master())(int) and returns void


Maybe you want to add a few typedef's to your code and make it clearer:

typedef void (fun)(); /* fun is of type "function accepting
unspecified parameters and returning
void" */
typedef fun * pfun; /* pfun is of type "pointer to fun" */

pfun master(void) /* master() returns a "pointer to fun" */
 
B

Barry Schwarz

Hi all,

I have come across a piece of code wherein a function returns a
function pointer as it seems to me but not very clear from the
prototype.

As shown below - although return type is void - (*master())() returns a

No, the return type is not void. See below
function pointer some_func & meanwhile calls that function and prints
the message "hi from some_func"

please help in interpreting this definition of "master" "function"

-------------------
#include<stdio.h>

void some_func()
{
printf("hi from some_func");
}

void (*master())(int)

You start at the name moving to the right but reverse directions every
time you hit an "unmatched" parenthesis. So
master is a function (whose parameters are unspecified)
that returns a pointer to
a function that takes a single int parameter
and returns void.

master does not return void. It returns a pointer to a function. The
function which that pointer points will return void.

If I had my druthers, I would declare it as
typedef void func_t(int);
func_t *master(void)
or
typedef void (*func_ptr)(int);
func_ptr master(void)
{
return some_func;
}

int main()
{
(*master())(7);

Call master with no arguments. The return value is a pointer to
some_func. Invoke some_func with the single argument 7.
return 0;
}


Remove del for email
 
R

ranjmis

Lot of nice explanations.
I loved the ones shown below

Ben said:
typedef void (*fn_t)();
really typedef solved lot of confusion
int main()
{
fn_t fn = master();
its clear that master returns a function pointer
here i called that function. earlier i believed that i am just
returning a function pointer why its getting called & printing the
message.
return 0;
}


master master
master() is a function without accepting
unspecified parameters
*master() that returns a pointer
(*master()) to a function
(*master())(int) that accepts one `int` parameter
void (*master())(int) and returns void
solves lot of problems for future

Barry said:
You start at the name moving to the right but reverse directions every
time you hit an "unmatched" parenthesis. So
master is a function (whose parameters are unspecified)
that returns a pointer to
a function that takes a single int parameter
and returns void.
this was really awesome. very clean way of reading

thanks!
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top