Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning a function pointer

Reply
Thread Tools

Returning a function pointer

 
 
Antoninus Twink
Guest
Posts: n/a
 
      08-04-2007
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.



 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      08-04-2007
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;
}
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-04-2007
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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-04-2007
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
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-04-2007
Antoninus Twink <(E-Mail Removed)> 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?
 
Reply With Quote
 
Colonel
Guest
Posts: n/a
 
      08-05-2007
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 == 9
pre = func_1;
return pre; //Return the pointer we want.
}

Exegesis: Visual C++ 6.0
"Antoninus Twink" <(E-Mail Removed)> 写入消息新闻:(E-Mail Removed) ...
> 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.
>
>
>



 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-05-2007
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 ---
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-05-2007
On Sun, 5 Aug 2007 17:06:58 +0800, "Colonel" <(E-Mail Removed)>
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 == 9
> 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" <(E-Mail Removed)> 写入消息新闻:(E-Mail Removed) ...
>> 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
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      08-06-2007
On Aug 4, 5:10 pm, Antoninus Twink <(E-Mail Removed)> 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;
}

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-07-2007
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:

> On Aug 4, 5:10 pm, Antoninus Twink <(E-Mail Removed)> 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 (?)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring static function returning pointer to extern function pembed2012 C Programming 1 02-27-2012 08:21 PM
function returning function pointer (recursive type) Mark Piffer C Programming 9 05-15-2009 07:54 AM
Function returning a function pointer? Protoman C++ 14 12-11-2005 07:11 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments