Can I passing a funtion pointer to a FUNTION which point to THE FUNTION?

Discussion in 'C Programming' started by Lee Xuzhang, Jun 12, 2006.

  1. Lee Xuzhang

    Lee Xuzhang Guest

    /* from SICP -- Exercise 4.21:
    ((lambda (n)
    ((lambda (fact)
    (fact fact n))
    (lambda (ft k)
    (if (= k 1)
    1
    (* k (ft ft (- k 1)))))))
    10)
    */


    /* I want to do the same thing like that in C/C++. */

    #include <stdio.h>

    /* I find that I can't declare the function what I want, in standard
    C/C++.
    * My colleague try the following code. It's OK, but I find something
    wrong.
    */
    int f( int (*g)(), int x )
    {
    if (x==0)
    return 1;
    else
    return x * (*g)(g, x-1);
    }


    int h(void)
    {
    return 2;
    }


    int main()
    {
    printf("%d\n", f(f,5)); /* This is what I want. */

    /* This is not I want; but the complier didn't warning. */
    printf("%d\n", f(h,5));

    return 0;
    }


    ==========

    /* I find a way to achieve it indirectly, but I do not like it. */
    #include <stdio.h>

    typedef struct scm {
    int (*fp)(struct scm, int);
    } SCM;

    int f(SCM g, int x)
    {
    if (x==0)
    return 1;
    else
    return x * (g.fp)(g, x-1);
    }


    int main()
    {
    SCM F;
    F.fp = f;
    printf("%d\n", f(F,5)); /* It's ugly.*/

    return 0;
    }


    /* I am sorry for my poor english. I hope you can understand what I
    want.
    * I just want to try wrinting "continuation" in C/C++.
    *
    * This is my question:
    * Can I do the same thing like this in standard C/C++?
    * Can I passing a funtion pointer to a FUNTION which point to THE
    FUNTION?
    */
    Lee Xuzhang, Jun 12, 2006
    #1
    1. Advertising

  2. Lee Xuzhang said:

    <Lithp thnipped>

    > /* I want to do the same thing like that in C/C++. */


    This is a Frequently-Asked Question, or FAQ. Steve Summit maintains a list
    of answers to such questions. The relevant one for you can be found here:

    <http://c-faq.com/decl/recurfuncp.html>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jun 12, 2006
    #2
    1. Advertising

  3. Lee Xuzhang

    Haider Guest

    Lee Xuzhang wrote:
    > /* from SICP -- Exercise 4.21:
    > ((lambda (n)
    > ((lambda (fact)
    > (fact fact n))
    > (lambda (ft k)
    > (if (= k 1)
    > 1
    > (* k (ft ft (- k 1)))))))
    > 10)
    > */
    >
    >
    > /* I want to do the same thing like that in C/C++. */
    >
    > #include <stdio.h>
    >
    > /* I find that I can't declare the function what I want, in standard
    > C/C++.
    > * My colleague try the following code. It's OK, but I find something
    > wrong.
    > */
    > int f( int (*g)(), int x )
    > {
    > if (x==0)
    > return 1;
    > else
    > return x * (*g)(g, x-1);
    > }
    >
    >
    > int h(void)
    > {
    > return 2;
    > }
    >
    >

    /* try the following change */
    typedef int(*fp)(void*, int);
    int fun( fp x , int y)
    {
    if(y ==0 )
    return 1;
    else
    return y * (*x)(x, y-1);
    }
    > int main()
    > {
    > printf("%d\n", f(f,5)); /* This is what I want. */
    >

    printf("%d\n", fun(fun,5)); /*work fine*/

    > /* This is not I want; but the complier didn't warning. */
    > printf("%d\n", f(h,5));


    printf("%d\n", fun(h,5)); /*compiler will give
    warning*/
    >
    > return 0;
    > }
    >
    >
    > ==========
    >
    > /* I find a way to achieve it indirectly, but I do not like it. */
    > #include <stdio.h>
    >
    > typedef struct scm {
    > int (*fp)(struct scm, int);
    > } SCM;
    >
    > int f(SCM g, int x)
    > {
    > if (x==0)
    > return 1;
    > else
    > return x * (g.fp)(g, x-1);
    > }
    >
    >
    > int main()
    > {
    > SCM F;
    > F.fp = f;
    > printf("%d\n", f(F,5)); /* It's ugly.*/
    >
    > return 0;
    > }
    >
    >
    > /* I am sorry for my poor english. I hope you can understand what I
    > want.
    > * I just want to try wrinting "continuation" in C/C++.
    > *
    > * This is my question:
    > * Can I do the same thing like this in standard C/C++?
    > * Can I passing a funtion pointer to a FUNTION which point to THE
    > FUNTION?
    > */
    Haider, Jun 12, 2006
    #3
  4. Lee Xuzhang wrote:
    > int f( int (*g)(), int x )


    It's ok. The first parameter g is type of the abstract type int (*) ().
    Read the prototype of the library function signal through man signal.
    H&S5 ยง9.2 provides two forms of the prototype of signal.

    > return x * (*g)(g, x-1);


    How can two arguments be passed into the function? Function call and
    prototype are mismatch.

    --
    lovecreatesbeauty
    lovecreatesbeauty, Jun 12, 2006
    #4
  5. Lee Xuzhang

    Lee Xuzhang Guest

    Thank you.

    To Haider:
    Your code is work fine in C, but it can't work in C++.

    And, from FAQ 4.13:
    "...void *'s are only guaranteed to hold object (i.e. data) pointers;
    it is not portable to convert a function pointer to type void *. (On
    some machines, function addresses can be very large, bigger than any
    data pointers.) ..."
    (Thanks for Richard Heathfield)

    Is "typedef int(*fp)(void*, int); " still right?


    Maybe I can't quite do it directly.
    (http://c-faq.com/decl/recurfuncp.html)
    Thanks for all.
    Lee Xuzhang, Jun 14, 2006
    #5
  6. On 14 Jun 2006 05:10:49 -0700, "Lee Xuzhang" <> wrote:
    >Your code is work fine in C, but it can't work in C++.


    You'll have to ask in a C++ group.

    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Kevin D. Quitt, Jun 14, 2006
    #6
    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. Guest
    Replies:
    6
    Views:
    2,673
    Siemel Naran
    Jul 15, 2004
  2. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  3. Amit_Basnak
    Replies:
    3
    Views:
    310
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jan 11, 2007
  4. Replies:
    10
    Views:
    583
    Tim Rentsch
    Nov 10, 2008
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page