Defining an array of pointer to function

Discussion in 'C++' started by Rahul, Oct 5, 2010.

  1. Rahul

    Rahul Guest

    Hi,

    I want to write an array of pointer to function "which takes an array
    of function pointer of type "int fun(int)" and returns a fptr of same
    type

    The following works well

    typedef int (*fp)(int);
    fp (*newFp[5]) (fp [5]);

    But when I try to inline the typedefs then it gives me compilation
    error in VC++ 2010 saying " syntax error : ')'"

    int (*)(int) (*newFp[5]) ( int (*[5]) (int) )

    How do I inline the declarations to remove the typedef (its not
    required but am asking just out of curiosity)

    Thanks in advance
    Rahul
    Rahul, Oct 5, 2010
    #1
    1. Advertising

  2. * Rahul, on 05.10.2010 11:38:
    >
    > I want to write an array of pointer to function "which takes an array
    > of function pointer of type "int fun(int)" and returns a fptr of same
    > type
    >
    > The following works well
    >
    > typedef int (*fp)(int);
    > fp (*newFp[5]) (fp [5]);
    >
    > But when I try to inline the typedefs then it gives me compilation
    > error in VC++ 2010 saying " syntax error : ')'"
    >
    > int (*)(int) (*newFp[5]) ( int (*[5]) (int) )
    >
    > How do I inline the declarations to remove the typedef (its not
    > required but am asking just out of curiosity)


    Well it sounds like homework, but it's often asked and seldom answered.

    Starting at the innermost thing, the name of the array:

    newFp

    What you can do with the array is to index it:

    newFp[]

    The result of that is a function pointer, which can be dereferenced:

    *newFp[]

    Yielding an untyped entity (corresponding to a delegate in other languages) that
    can be called:

    (*newFp[])(...)

    The result of calling is a function pointer that can be dereferenced:

    *(*newFp[])(...)

    And called with int argument:

    (*(*newFp[])(...))(int)

    Yielding an int:

    int (*(*newFp[])(...))(int)

    The dots formal argument should be an array, and such array can be indexed:

    int (*(*newFp[])(...[]))(int)

    The result of indexing is a function pointer that can be dereferenced:

    int (*(*newFp[])(...*...[]))(int)

    And called with int argument:

    int (*(*newFp[])(...(*[])(int) ))(int)

    Yielding an int:

    int (*(*newFp[])(int (*[])(int)))(int)

    Given this it's understandable why both Brian Kernighan (I think it was) and
    Bjarne Stroustrup have described the C declaration syntax as a "failed
    experiment". :)

    In short, use typedef.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Oct 5, 2010
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Oct 5, 5:00 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * Rahul, on 05.10.2010 11:38:
    >
    >
    >
    >
    >
    >
    >
    > > I want to write an array of pointer to function "which takes an array
    > > of function pointer of type "int fun(int)" and returns a fptr of same
    > > type

    >
    > > The following works well

    >
    > > typedef int (*fp)(int);
    > > fp (*newFp[5]) (fp [5]);

    >
    > > But when I try to inline the typedefs then it gives me compilation
    > > error in VC++ 2010 saying " syntax error : ')'"

    >
    > > int (*)(int)  (*newFp[5])  ( int (*[5]) (int) )

    >
    > > How do I inline the declarations to remove the typedef (its not
    > > required but am asking just out of curiosity)

    >
    > Well it sounds like homework, but it's often asked and seldom answered.
    >
    > Starting at the innermost thing, the name of the array:
    >
    >    newFp
    >
    > What you can do with the array is to index it:
    >
    >    newFp[]
    >
    > The result of that is a function pointer, which can be dereferenced:
    >
    >    *newFp[]
    >
    > Yielding an untyped entity (corresponding to a delegate in other languages) that
    > can be called:
    >
    >    (*newFp[])(...)
    >
    > The result of calling is a function pointer that can be dereferenced:
    >
    >    *(*newFp[])(...)
    >
    > And called with int argument:
    >
    >    (*(*newFp[])(...))(int)
    >
    > Yielding an int:
    >
    >    int (*(*newFp[])(...))(int)
    >
    > The dots formal argument should be an array, and such array can be indexed:
    >
    >    int (*(*newFp[])(...[]))(int)
    >
    > The result of indexing is a function pointer that can be dereferenced:
    >
    >    int (*(*newFp[])(...*...[]))(int)
    >
    > And called with int argument:
    >
    >    int (*(*newFp[])(...(*[])(int) ))(int)
    >
    > Yielding an int:
    >
    >    int (*(*newFp[])(int (*[])(int)))(int)
    >
    > Given this it's understandable why both Brian Kernighan (I think it was) and
    > Bjarne Stroustrup have described the C declaration syntax as a "failed
    > experiment". :)
    >
    > In short, use typedef.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>


    Thanks Alf!!

    I got the funda.
    Rahul, Oct 5, 2010
    #3
  4. Rahul

    Puppet_Sock Guest

    On Oct 5, 8:00 am, "Alf P. Steinbach /Usenet"
    [Alf explains a complicated declaration]

    Whew! Yes indeed, thanks Alf. That was a ride.
    Socks
    Puppet_Sock, Oct 5, 2010
    #4
    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. johny smith
    Replies:
    8
    Views:
    411
    Peter Koch Larsen
    Jul 2, 2004
  2. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    346
    Tim Rentsch
    Oct 9, 2004
  3. Spiro Trikaliotis

    Array initialization when defining the array

    Spiro Trikaliotis, Jun 8, 2007, in forum: C Programming
    Replies:
    5
    Views:
    320
    Spiro Trikaliotis
    Jun 9, 2007
  4. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    673
    Martin Ambuhl
    Jan 28, 2008
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page