Returning a function pointer

Discussion in 'C Programming' started by Antoninus Twink, Aug 4, 2007.

  1. 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.

    :(
     
    Antoninus Twink, Aug 4, 2007
    #1
    1. Advertising

  2. Antoninus Twink

    jacob navia Guest

    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;
    }
     
    jacob navia, Aug 4, 2007
    #2
    1. Advertising

  3. Antoninus Twink

    CBFalconer Guest

    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
     
    CBFalconer, Aug 4, 2007
    #3
  4. 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 Heathfield, Aug 5, 2007
    #4
  5. Antoninus Twink

    Richard Guest

    Antoninus Twink <> 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?
     
    Richard, Aug 5, 2007
    #5
  6. Antoninus Twink

    Colonel Guest

    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" <> дÈëÏûÏ¢ÐÂÎÅ:...
    > 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.
    >
    > :(
    >
     
    Colonel, Aug 5, 2007
    #6
  7. Antoninus Twink

    Joe Wright Guest

    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 ---
     
    Joe Wright, Aug 5, 2007
    #7
  8. On Sun, 5 Aug 2007 17:06:58 +0800, "Colonel" <>
    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" <> дÈëÏûÏ¢ÐÂÎÅ:...
    >> 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
     
    Barry Schwarz, Aug 5, 2007
    #8
  9. Antoninus Twink

    John Bode Guest

    On Aug 4, 5:10 pm, 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.
    >
    > :(


    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;
    }
     
    John Bode, Aug 6, 2007
    #9
  10. Antoninus Twink

    Army1987 Guest

    On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:

    > On Aug 4, 5:10 pm, 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.
    >>
    >> :(

    >
    > 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 (?)
     
    Army1987, Aug 7, 2007
    #10
  11. Antoninus Twink

    John Bode Guest

    On Aug 6, 7:20 pm, Army1987 <> wrote:
    > On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:
    > > On Aug 4, 5:10 pm, 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.

    >
    > >> :(

    >
    > > 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 (?)
     
    John Bode, Aug 7, 2007
    #11
  12. On Sun, 05 Aug 2007 10:23:50 -0400, Joe Wright
    <> wrote:
    <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
     
    David Thompson, Aug 26, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    708
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,052
    Fraser Ross
    Aug 14, 2004
  3. Protoman
    Replies:
    14
    Views:
    568
    Protoman
    Dec 11, 2005
  4. Mark Piffer
    Replies:
    9
    Views:
    911
    luserXtrog
    May 15, 2009
  5. pembed2012
    Replies:
    1
    Views:
    427
    James Kuyper
    Feb 27, 2012
Loading...

Share This Page