[Function pointer] Translating these typedefs into one

Discussion in 'C++' started by mailforpr@googlemail.com, Oct 12, 2006.

  1. Guest

    typedef void(*int_arg)(int);
    typedef int_arg(*pf)(int*,int*,int_arg);

    I tried this:

    typedef void(*)(int)(*ultra)(int*,int*,void(*)(int));

    and got a syntax error.
    , Oct 12, 2006
    #1
    1. Advertising

  2. Ron Natalie Guest

    wrote:
    > typedef void(*int_arg)(int);
    > typedef int_arg(*pf)(int*,int*,int_arg);
    >
    > I tried this:
    >
    > typedef void(*)(int)(*ultra)(int*,int*,void(*)(int));
    >
    > and got a syntax error.
    >


    No you got the return type wrong. You need to substitute
    the rest of the function declaration where int_arg goes in
    the original typedef:


    typedef void (*(*ultra)(int*, int*, int_arg))(int);
    Ron Natalie, Oct 12, 2006
    #2
    1. Advertising

  3. wrote:
    > typedef void(*int_arg)(int);
    > typedef int_arg(*pf)(int*,int*,int_arg);
    >
    > I tried this:
    >
    > typedef void(*)(int)(*ultra)(int*,int*,void(*)(int));
    >
    > and got a syntax error.
    >


    Try this:

    typedef void (*ultra(int*, int*, void(*)(int)))(int);

    Krishanu
    Krishanu Debnath, Oct 12, 2006
    #3
  4. (1) typedef void (*int_arg)(int);

    This is a pointer to a function which takes an int and returns void.


    (2) typedef int_arg (*pf)(int*,int*,int_arg);

    This is a pointer to a function which takes three arguments:

    int*
    [ii] int*
    [iii] void (*)(int)

    And whose return type is:

    void (*)(int);

    Here's how we put them together. First of all, start off with the function
    return type, which is ultimately the "fundamental type" of the entire
    declaration:

    void (*pf)(int);

    From here, we turn it into a function by putting parentheses directly after
    the name, and filling these parentheses with the function parameter types:

    void ( *pf(int*,int*,void(*)(int)) )(int)

    We now turn this into a pointer to what it already is by enclosing the name
    in parentheses with an asterisk:

    typedef void ( *(*pf)(int*,int*,void(*)(int)) )(int);

    As follows:

    typedef void (*int_arg)(int);

    /* typedef int_arg (*pf)(int*,int*,int_arg); */

    typedef void ( *(*pf)(int*,int*,void(*)(int)) )(int);

    int main()
    {
    int_arg x;

    pf p;

    x = p(0,0,x);
    }

    --



    Frederick Gotham
    Frederick Gotham, Oct 12, 2006
    #4
  5. Ron Natalie Guest

    Krishanu Debnath wrote:
    > wrote:
    >> typedef void(*int_arg)(int);
    >> typedef int_arg(*pf)(int*,int*,int_arg);
    >>
    >> I tried this:
    >>
    >> typedef void(*)(int)(*ultra)(int*,int*,void(*)(int));
    >>
    >> and got a syntax error.
    >>

    >
    > Try this:
    >
    > typedef void (*ultra(int*, int*, void(*)(int)))(int);
    >
    > Krishanu

    He could try it, but it wouldn't be right
    *ultra( ... ) is a function returning pointer
    not pointer to function. You need one more set of parens.
    Ron Natalie, Oct 12, 2006
    #5
  6. Ron Natalie wrote:
    > Krishanu Debnath wrote:
    >> wrote:
    >>> typedef void(*int_arg)(int);
    >>> typedef int_arg(*pf)(int*,int*,int_arg);
    >>>
    >>> I tried this:
    >>>
    >>> typedef void(*)(int)(*ultra)(int*,int*,void(*)(int));
    >>>
    >>> and got a syntax error.
    >>>

    >>
    >> Try this:
    >>
    >> typedef void (*ultra(int*, int*, void(*)(int)))(int);
    >>
    >> Krishanu

    > He could try it, but it wouldn't be right
    > *ultra( ... ) is a function returning pointer
    > not pointer to function. You need one more set of parens.


    You are right of course. Thanks for the correction.

    Krishanu
    Krishanu Debnath, Oct 12, 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. Oxnard
    Replies:
    4
    Views:
    343
    Joona I Palaste
    Oct 26, 2004
  2. emerth
    Replies:
    3
    Views:
    372
    emerth
    Aug 8, 2003
  3. Replies:
    12
    Views:
    498
    Skarmander
    Jun 24, 2006
  4. Replies:
    1
    Views:
    368
    Victor Bazarov
    Jan 25, 2008
  5. DaJones

    Pointer typedefs?

    DaJones, Feb 25, 2009, in forum: C++
    Replies:
    16
    Views:
    627
    Jorgen Grahn
    Mar 10, 2009
Loading...

Share This Page