array of pointers on functions

Discussion in 'C Programming' started by Evangelista Sami, Oct 4, 2003.

  1. hello

    i haven't touch any C code for long time and i dont remember how to
    declare an array of pointers on functions. i have tried this :
    -----------------------
    35 void firing_1
    36 (firing f,
    37 int *old_m,
    38 int *new_m);
    39
    40 const void (*firing_table[1])
    41 (firing,
    42 int *,
    43 int *) =
    44 {
    45 firing_1
    46 };
    ------------------------

    but the compiler says :
    __firings__.h:46: warning: initialization from incompatible pointer
    type

    my gcc version is 2.8.1.

    any idea?

    thanks for any response
     
    Evangelista Sami, Oct 4, 2003
    #1
    1. Advertising

  2. Evangelista Sami wrote:

    > hello
    >
    > i haven't touch any C code for long time and i dont remember how to
    > declare an array of pointers on functions. i have tried this :
    > -----------------------
    > 35 void firing_1
    > 36 (firing f,
    > 37 int *old_m,
    > 38 int *new_m);
    > 39
    > 40 const void (*firing_table[1])
    > 41 (firing,
    > 42 int *,
    > 43 int *) =
    > 44 {
    > 45 firing_1
    > 46 };
    > ------------------------
    >
    > but the compiler says :
    > __firings__.h:46: warning: initialization from incompatible pointer
    > type


    Typedefs usually help in this kind of situation.

    typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

    FIRING_FUNCTION firing_1;

    int main(void)
    {
    const FIRING_FUNCTION *firing_table[1];

    firing_table[0] = firing_1;

    return 0;
    }


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Oct 4, 2003
    #2
    1. Advertising

  3. Evangelista Sami

    pete Guest

    Evangelista Sami wrote:
    >
    > hello
    >
    > i haven't touch any C code for long time and i dont remember how to
    > declare an array of pointers on functions. i have tried this :
    > -----------------------
    > 35 void firing_1
    > 36 (firing f,
    > 37 int *old_m,
    > 38 int *new_m);
    > 39
    > 40 const void (*firing_table[1])
    > 41 (firing,
    > 42 int *,
    > 43 int *) =
    > 44 {
    > 45 firing_1
    > 46 };
    > ------------------------
    >
    > but the compiler says :
    > __firings__.h:46: warning: initialization from incompatible pointer
    > type
    >
    > my gcc version is 2.8.1.
    >
    > any idea?/* BEGIN new.c */


    #include <stdio.h>
    #include <math.h>

    #define FUNCTION_LIST { sin , cos, tan }
    #define STRING_LIST {"sin", "cos", "tan"}
    #define PI_OVER_4 (3.14159265 / 4)
    #define FUNCTIONS (sizeof function / sizeof *function)

    int main(void)
    {
    double (*function[])(double) = FUNCTION_LIST;
    char *string[] = STRING_LIST;
    size_t result;

    for (result = 0; result != FUNCTIONS; ++result) {
    printf("%s(%f) is %f\n",
    string[result], PI_OVER_4, function[result](PI_OVER_4));
    }
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Oct 4, 2003
    #3
  4. Evangelista Sami wrote:
    >
    > hello
    >
    > i haven't touch any C code for long time and i dont remember how to
    > declare an array of pointers on functions. i have tried this :
    > -----------------------
    > 35 void firing_1
    > 36 (firing f,
    > 37 int *old_m,
    > 38 int *new_m);
    > 39
    > 40 const void (*firing_table[1])
    > 41 (firing,
    > 42 int *,
    > 43 int *) =
    > 44 {
    > 45 firing_1
    > 46 };
    > ------------------------
    >
    > but the compiler says :
    > __firings__.h:46: warning: initialization from incompatible pointer
    > type
    >
    > my gcc version is 2.8.1.
    >
    > any idea?
    >
    > thanks for any response


    Jirka Klaue kindly showed me this:

    double (*f[])(double) = {sin, cos, log, exp}; // array of pointers
    for (i=0; i<4; i++) printf("%f\n", f(.5)); // ex. of use

    The functions can be anything, as long as they are consistent
    in the type(s) of their argument(s), and in the type they return.

    Probably a good idea to prototype any functions you want to point
    to _before_ you point to them in the table ;-)

    --
    Julian V. Noble
    Professor Emeritus of Physics

    ^^^^^^^^^^^^^^^^^^
    http://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Oct 4, 2003
    #4
  5. Evangelista Sami wrote:
    >
    > hello
    >
    > i haven't touch any C code for long time and i dont remember how to
    > declare an array of pointers on functions. i have tried this :
    > -----------------------
    > 35 void firing_1
    > 36 (firing f,
    > 37 int *old_m,
    > 38 int *new_m);
    > 39
    > 40 const void (*firing_table[1])
    > 41 (firing,
    > 42 int *,
    > 43 int *) =
    > 44 {
    > 45 firing_1
    > 46 };
    > ------------------------
    >
    > but the compiler says :
    > __firings__.h:46: warning: initialization from incompatible pointer
    > type
    >
    > my gcc version is 2.8.1.
    >
    > any idea?
    >
    > thanks for any response


    Jirka Klaue kindly showed me this:

    double (*f[])(double) = {sin, cos, log, exp}; // array of pointers
    for (i=0; i<4; i++) printf("%f\n", f(.5)); // ex. of use

    The functions can be anything, as long as they are consistent
    in the type(s) of their argument(s), and in the type they return.

    Probably a good idea to prototype any functions you want to point
    to _before_ you point to them in the table ;-)

    --
    Julian V. Noble
    Professor Emeritus of Physics

    ^^^^^^^^^^^^^^^^^^
    http://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Oct 4, 2003
    #5
  6. On 4 Oct 2003 04:33:20 -0700, (Evangelista Sami)
    wrote:

    >hello
    >
    >i haven't touch any C code for long time and i dont remember how to
    >declare an array of pointers on functions. i have tried this :
    >-----------------------
    >35 void firing_1
    >36 (firing f,
    >37 int *old_m,
    >38 int *new_m);
    >39
    >40 const void (*firing_table[1])
    >41 (firing,
    >42 int *,
    >43 int *) =
    >44 {
    >45 firing_1
    >46 };
    >------------------------
    >
    >but the compiler says :
    >__firings__.h:46: warning: initialization from incompatible pointer
    >type
    >
    >my gcc version is 2.8.1.
    >
    >any idea?
    >
    >thanks for any response


    Mine processed it without any diagnostics at all once I provided a
    typedef for firing. Did you cut and paste the code or re-type it?


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 5, 2003
    #6
  7. Evangelista Sami

    Micah Cowan Guest

    (Evangelista Sami) writes:

    > hello
    >
    > i haven't touch any C code for long time and i dont remember how to
    > declare an array of pointers on functions. i have tried this :
    > -----------------------
    > 35 void firing_1
    > 36 (firing f,
    > 37 int *old_m,
    > 38 int *new_m);
    > 39
    > 40 const void (*firing_table[1])
    > 41 (firing,
    > 42 int *,
    > 43 int *) =
    > 44 {
    > 45 firing_1
    > 46 };
    > ------------------------
    >
    > but the compiler says :
    > __firings__.h:46: warning: initialization from incompatible pointer
    > type
    >
    > my gcc version is 2.8.1.
    >
    > any idea?
    >
    > thanks for any response


    You have an array[1] of pointers to functions... returning const
    void?!?

    Maybe you wanted:

    void (* const firing_table[1])(firing, int *, int *) = { firing_1 };

    Array[1] of const pointers to functions... returning void.

    BTW, what good is a single-element array in this case?

    -Micah
     
    Micah Cowan, Oct 5, 2003
    #7
  8. "Richard Heathfield" <> wrote in message
    news:blmcab$1nc$...
    > Evangelista Sami wrote:
    >
    > > hello
    > >
    > > i haven't touch any C code for long time and i dont remember how to
    > > declare an array of pointers on functions. i have tried this :
    > > -----------------------
    > > 35 void firing_1
    > > 36 (firing f,
    > > 37 int *old_m,
    > > 38 int *new_m);
    > > 39
    > > 40 const void (*firing_table[1])
    > > 41 (firing,
    > > 42 int *,
    > > 43 int *) =
    > > 44 {
    > > 45 firing_1
    > > 46 };
    > > ------------------------
    > >
    > > but the compiler says :
    > > __firings__.h:46: warning: initialization from incompatible pointer
    > > type

    >
    > Typedefs usually help in this kind of situation.
    >
    > typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);
    >
    > FIRING_FUNCTION firing_1;


    I have an aversion to declaring function prototypes like that as they look
    far too much like object declarations. YMMV

    > int main(void)
    > {
    > const FIRING_FUNCTION *firing_table[1];


    I'm not so sure that const is a good idea.

    6.7.3p8: "...If the specification of a function type includes any type
    qualifiers, the behavior is undefined."

    > firing_table[0] = firing_1;


    This assignment would presumably require a diagnostic as it discards
    qualifiers.

    >
    > return 0;
    > }


    --
    Peter
     
    Peter Nilsson, Oct 5, 2003
    #8
  9. Peter Nilsson wrote:

    > "Richard Heathfield" <> wrote in message
    > news:blmcab$1nc$...
    >>
    >> Typedefs usually help in this kind of situation.
    >>
    >> typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);
    >>
    >> FIRING_FUNCTION firing_1;

    >
    > I have an aversion to declaring function prototypes like that as they look
    > far too much like object declarations. YMMV


    MMDIV, yes. My counter-aversion is to concealing pointer types inside
    typedefs.

    >
    >> int main(void)
    >> {
    >> const FIRING_FUNCTION *firing_table[1];

    >
    > I'm not so sure that const is a good idea.


    Neither am I. Not sure how it got in there. Sorry about that.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Oct 5, 2003
    #9
  10. "Richard Heathfield" <> wrote in message
    news:blocn9$duh$...
    > Peter Nilsson wrote:
    > > "Richard Heathfield" <> wrote in message
    > >>
    > >> typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);
    > >>
    > >> FIRING_FUNCTION firing_1;

    > >
    > > I have an aversion to declaring function prototypes like that as they

    look
    > > far too much like object declarations. YMMV

    >
    > MMDIV, yes. ...


    Why not use the far less ambiguous explicit prototype...?

    void firing_1(firing, int *, int *);

    What do you gain from the use of the typedef in a function declaration?

    --
    Peter
     
    Peter Nilsson, Oct 5, 2003
    #10
  11. Peter Nilsson wrote:

    > "Richard Heathfield" <> wrote in message
    > news:blocn9$duh$...
    >> Peter Nilsson wrote:
    >> >
    >> > I have an aversion to declaring function prototypes
    >> > like that as they look
    >> > far too much like object declarations. YMMV

    >>
    >> MMDIV, yes. ...

    >
    > Why not use the far less ambiguous explicit prototype...?
    >
    > void firing_1(firing, int *, int *);
    >
    > What do you gain from the use of the typedef in a function declaration?


    It makes defining arrays of function pointers rather simpler. In some
    circumstances, it can also make the relationship between a bunch of
    function prototypes more obvious. Example from some recent demo code for
    Win32:

    /* this is in a library header */
    typedef LRESULT MW_MESSAGE_HANDLER(HWND,
    UINT,
    WPARAM,
    LPARAM);

    /* these are in an application header */
    MW_MESSAGE_HANDLER VigenereCreate;
    MW_MESSAGE_HANDLER VigenereResize;
    MW_MESSAGE_HANDLER VigenereCommand;
    MW_MESSAGE_HANDLER VigenereDestroy;

    But no, I don't typedef every single function type that I use. I do it when
    it seems to me to be The Right Thing.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    Rudimentary Win32 programming tutorial:
    http://www.rjgh.co.uk/prg/c/windows/index.html
     
    Richard Heathfield, Oct 5, 2003
    #11
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,989
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    884
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    328
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
  5. ajaybgr
    Replies:
    18
    Views:
    1,090
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page