By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

Returning a function pointer

P: n/a
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.

:(

Aug 4 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Antoninus Twink wrote:
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;
}
Aug 4 '07 #2

P: n/a
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;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 4 '07 #3

P: n/a
Antoninus Twink wrote:
>
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 ...
};

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
--
Posted via a free Usenet account from http://www.teranews.com

Aug 4 '07 #4

P: n/a
Antoninus Twink <sp*****@invalid.comwrites:
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?
Aug 4 '07 #5

P: n/a
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
"Antoninus Twink" <sp*****@invalid.com写入消息新闻:sl*************** *****@nospam.invalid...
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.

:(

Aug 5 '07 #6

P: n/a
Antoninus Twink wrote:
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;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 5 '07 #7

P: n/a
On Sun, 5 Aug 2007 17:06:58 +0800, "Colonel" <xi**********@163.com>
wrote:
>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
"Antoninus Twink" <sp*****@invalid.com写入消息新闻:sl*************** *****@nospam.invalid...
>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.

:(

Remove del for email
Aug 5 '07 #8

P: n/a
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.comwrote:
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;
}

Aug 6 '07 #9

P: n/a
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.comwrote:
>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);
What happens the fourth time the loop body is executed? :-)
if (result)
{
printf("result = %s\n", result);
}
}

return 0;
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #10

P: n/a
On Aug 6, 7:20 pm, Army1987 <army1...@NOSPAM.itwrote:
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.comwrote:
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);

What happens the fourth time the loop body is executed? :-)
Something wonderful and unexpected.
>
if (result)
{
printf("result = %s\n", result);
}
}
return 0;
}

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #11

P: n/a
On Sun, 05 Aug 2007 10:23:50 -0400, Joe Wright
<jo********@comcast.netwrote:
<snip>
How about this?
<snip>
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
Aug 26 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.