Array of function pointers

Discussion in 'C Programming' started by sushil, Dec 4, 2003.

  1. sushil

    sushil Guest

    +1 #include<stdio.h>
    +2 #include <stdlib.h>
    +3 typedef struct
    +4 {
    +5 unsigned int PID;
    +6 unsigned int CID;
    +7 } T_ID;
    +8
    +9 typedef unsigned int (*T_HANDLER)(void);
    +10
    +11 void Display1(void);
    +12 void Display2(void);
    +13
    +14 int main()
    +15 {
    +16 T_HANDLER A_HANDLER[3] = { (T_HANDLER)Display1,
    +17 (T_HANDLER)Display2};
    +18
    +19 (A_HANDLER[0])();
    +20 (*A_HANDLER[1])();
    ^ Is this '*' required ?
    +21 return 1;
    +22 }
    +23
    +24 void Display1()
    +25 {
    +26 printf("\nDisplay1 called\n");
    +27 }
    +28
    +29 void Display2()
    +30 {
    +31 printf("\nDisplay2 called\n");
    +32 }
    Hi,
    I am new to group. I just tried to run a code with array of function pointer.
    I could not understand why there is no error due to '*' at line no. 20.
    Could anybody help me ?

    Regards,
    Sushil.
     
    sushil, Dec 4, 2003
    #1
    1. Advertising

  2. On 4 Dec 2003, sushil wrote:

    > +19 (A_HANDLER[0])();
    > +20 (*A_HANDLER[1])();
    > ^ Is this '*' required ?


    Not really, but it doesn't harm either.


    > I could not understand why there is no error due to '*' at line no. 20.
    > Could anybody help me ?


    Let's have

    void f(void) {}
    void (*pf)(void) = &f; /* ...= f goes as well. */

    We get:

    &f -> pointer to function
    f -> function
    pf -> pointer to function
    *pf -> function

    Now, like arrays, functions usually decay to pointers to them:

    (&f)() -> (pointer to function)()
    (f)() -> (function)() -> (pointer to function)()
    (pf)() -> (pointer to function)()
    (*pf)() -> (function)() -> (pointer to function)()

    As well we get:

    *(&f) -> *(pointer to function) -> function
    *(f) -> *(function) -> *(pointer to function) -> function
    *(pf) -> *(pointer to function) -> function
    *(*pf) -> *(function) -> *(pointer to function) -> function

    And further:

    *(*(&f)) -> *(function) -> *(pointer to function) -> function
    *(*(f)) -> *(function) -> *(pointer to function) -> function
    *(*(pf)) -> *(function) -> *(pointer to function) -> function
    *(*(*pf)) -> *(function) -> *(pointer to function) -> function

    etc.

    So you can pretty much replace any function() with a (******function)()
    and it's the same thing.

    The array you have is just an extra layer of complexity.
     
    Jarno A Wuolijoki, Dec 4, 2003
    #2
    1. Advertising

  3. On Thu, 4 Dec 2003, Jarno A Wuolijoki wrote:

    > Now, like arrays, functions usually decay to pointers to them:


    Oh, let's fix an "implied" thinko before anyone else catches:

    Arrays will of course decay to pointers to their elements, not
    to pointers to itself.
     
    Jarno A Wuolijoki, Dec 4, 2003
    #3
    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. Phil
    Replies:
    1
    Views:
    651
    llewelly
    Sep 16, 2003
  2. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    520
    Peter Goddard
    May 16, 2005
  3. Sean
    Replies:
    2
    Views:
    641
    loufoque
    Sep 24, 2006
  4. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    335
    Chris Torek
    Apr 6, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
Loading...

Share This Page