Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Returning a function pointer (http://www.velocityreviews.com/forums/t527035-returning-a-function-pointer.html)

Antoninus Twink 08-04-2007 10:10 PM

Returning a function pointer
 
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.

:(


jacob navia 08-04-2007 10:16 PM

Re: Returning a function pointer
 
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;
}

CBFalconer 08-04-2007 10:57 PM

Re: Returning a function pointer
 
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


Richard Heathfield 08-04-2007 11:07 PM

Re: Returning a function pointer
 
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

Richard 08-04-2007 11:14 PM

Re: Returning a function pointer
 
Antoninus Twink <spamoff@invalid.com> writes:

> 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?

Colonel 08-05-2007 09:06 AM

Re: Returning a function pointer
 
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" <spamoff@invalid.com> 写入消息新闻:slrnfb9u9n.vl5.spamoff@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.
>
> :(
>




Joe Wright 08-05-2007 02:23 PM

Re: Returning a function pointer
 
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 ---

Barry Schwarz 08-05-2007 04:25 PM

Re: Returning a function pointer
 
On Sun, 5 Aug 2007 17:06:58 +0800, "Colonel" <xilongfan373@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" <spamoff@invalid.com> 写入消息新闻:slrnfb9u9n.vl5.spamoff@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

John Bode 08-06-2007 09:45 PM

Re: Returning a function pointer
 
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.com> 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.
>
> :(


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;
}


Army1987 08-07-2007 12:20 AM

Re: Returning a function pointer
 
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:

> On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.com> 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.
>>
>> :(

>
> 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 (?)



All times are GMT. The time now is 07:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.