Returning a function pointer

  • Thread starter Antoninus Twink
  • Start date
A

Antoninus Twink

What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
 
J

jacob navia

Antoninus said:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

I have never managed to do it without a typedef...

char *fn1(int a)
{
return "function 1";
}

char *fn2(int a)
{
return "function 2";
}

typedef char *(*FunctionType)(int);
FunctionType FunctionReturningAFunctionPointer(int a)
{
if (a > 0)
return fn1;
else
return fn2;
}
 
C

CBFalconer

Antoninus said:
What's the correct syntax to define a function that returns a
pointer to a function? Specifically, I'd like a function that
takes an int, and returns a pointer to a function that takes an
int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

How about:

typedef char *stringfromint(int);
stringfromint *transfer(int) {
... amazing code ...
};
 
R

Richard Heathfield

Antoninus Twink said:
What's the correct syntax to define a function that returns a pointer
to a function? Specifically, I'd like a function that takes an int,
and returns a pointer to a function that takes an int and returns a
string.

char *foo(int x)
{
static char bar[2];
bar[0] = x;
return bar;
}

char *(*baz(int n))(int)
{
/* use n in some way, I guess */

return foo;
}
 
R

Richard

Antoninus Twink said:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

Could the tool cundecl help you?
 
C

Colonel

The function you have declared was right. I have a programme using it like
this:

#include <stdio.h>
char * (* func (int b)) (int); //The function returns a pointer.
char fun_1 (int a); //The function that the pointer pointed.
int main (void)
{
int b;
char ch;
char (*pre) (int a); //Declare an pointer point to func_1.

b = 98;

pre = (func (b)); //Get the pointer returned.
ch = (*pre) (b); //"pre" pointed to fun_1 ().
printf ( "ch = %c\n", ch);

return 0;
}
char func_1 (int a)
{
return a;
}
char * (* func (int b)) (int)
{
char (*pre) (int a);
if (b == 98)
pre = func_1;
return pre; //Return the pointer we want.
}

Exegesis: Visual C++ 6.0
 
J

Joe Wright

Antoninus said:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
How about this?

#include <stdio.h>

typedef char*(*fp_t)(int);

char glob[20];

char *foo(int i) {
sprintf(glob, "You've called foo with %d\n", i);
return glob;
}

char *bar(int i) {
sprintf(glob, "You've called bar with %d\n", i);
return glob;
}

fp_t baz(int i) {
fp_t ret;
if (i)
ret = foo;
else
ret = bar;
return ret;
}

int main(void) {
fp_t fun;
fun = baz(1);
puts(fun(42));
return 0;
}
 
B

Barry Schwarz

The function you have declared was right. I have a programme using it like
this:

#include <stdio.h>

If this is your actual code, you need to up the warning level of your
compiler and pay heed to the diagnostics.
char * (* func (int b)) (int); //The function returns a pointer.

The pointer that func returns points to a function. That function
returns a char*.
char fun_1 (int a); //The function that the pointer pointed.

fun_1 returns a char. Its address cannot be the return value from
func.
int main (void)
{
int b;
char ch;
char (*pre) (int a); //Declare an pointer point to func_1.

pre is a pointer to function that returns a char. It is compatible
with fun_1.
b = 98;

pre = (func (b)); //Get the pointer returned.

func returned a pointer to a function that returns a char*. It is
**not** compatible with pre.
ch = (*pre) (b); //"pre" pointed to fun_1 ().
printf ( "ch = %c\n", ch);

return 0;
}
char func_1 (int a)
{
return a;
}
char * (* func (int b)) (int)

Again, the pointer that func returns points to a function returning a
char*. (This is compatible with the prototype above.)
{
char (*pre) (int a);

Again, this local pre points to a function that returns char. It is
not compatible with the return type of func.
if (b == 98)
pre = func_1;

This is a constraint violation. The two operands of the assignment
operator are incompatible. There is no implicit conversion between
the two.
return pre; //Return the pointer we want.

It may be the pointer you want but it is the wrong type to return from
this function.
}

Exegesis: Visual C++ 6.0


Remove del for email
 
J

John Bode

What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */

}

but this doesn't seem to work.

:(

Define "doesn't seem to work." Are you getting a syntax error? A
runtime error? What?

f -- f
f() -- is a function
f(int n) -- that takes an integer
*f(int n) -- and returns a pointer
(*f(int n))() -- to a function
(*f(int n))(int m) -- that takes an integer
char *(*f(int n))(int m) -- and returns a char *

So, apart from the gint/gchar weirdness (I'm guessing this comes from
some API you're using), your definition looks all right to me.

char *foo(int m)
{
/* does something interesting */
}

char *bar(int m)
{
/* does something interesting */
}

char *bletch(int m)
{
/* does something interesting */
}

char *(*f(int n))(int m)
{
char *(*p)(int m);

switch(n)
{
case 0: p = foo; break;
case 1: p = bar; break;
case 2: p = bletch; break;
default: p = NULL; break;
}

return p;
}

int main(void)
{
char *result;
char *(*p)(int m);

int i;

for (i = 0; i < 4; i++)
{
p = f(i);
result = p(123);
if (result)
{
printf("result = %s\n", result);
}
}

return 0;
}
 
A

Army1987

Define "doesn't seem to work." Are you getting a syntax error? A
runtime error? What?

f -- f
f() -- is a function
f(int n) -- that takes an integer
*f(int n) -- and returns a pointer
(*f(int n))() -- to a function
(*f(int n))(int m) -- that takes an integer
char *(*f(int n))(int m) -- and returns a char *

So, apart from the gint/gchar weirdness (I'm guessing this comes from
some API you're using), your definition looks all right to me.

char *foo(int m)
{
/* does something interesting */
}

char *bar(int m)
{
/* does something interesting */
}

char *bletch(int m)
{
/* does something interesting */
}

char *(*f(int n))(int m)
{
char *(*p)(int m);

switch(n)
{
case 0: p = foo; break;
case 1: p = bar; break;
case 2: p = bletch; break;
default: p = NULL; break;
}

return p;
}

int main(void)
{
char *result;
char *(*p)(int m);

int i;

for (i = 0; i < 4; i++)
{
p = f(i);
result = p(123);
What happens the fourth time the loop body is executed? :)
 
D

David Thompson

On Sun, 05 Aug 2007 10:23:50 -0400, Joe Wright
How about this?
char glob[20];

char *foo(int i) {
sprintf(glob, "You've called foo with %d\n", i);
return glob;
}
<snip bar() similar and funcptr to one of them used>

glob needs to be 27 chars for the valued used (42), and should be
rather larger to be safe in general.

Even better use snprintf, standard in C99 and fairly common
before/without that, in case you do get it wrong.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top