Warnings calling functions in other object files by pointer

Discussion in 'C Programming' started by Andrew Slade, Dec 30, 2003.

  1. Andrew Slade

    Andrew Slade Guest

    Hello,

    I am making calls from one compilation unit to functions in another by
    pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The
    executable seems to work fine but the warnings bothers me a lot, mostly
    because I don't understand what I did wrong. The code I used I got from the
    FAQ on the matter, question 4.12.

    "warning: passing arg 2 of `hash_emumerate' from incompatible pointer type"
    "warning: passing arg 2 of `hash_free_table' from incompatible pointer
    type"

    Compilation unit that is calling, stub.o
    {
    ..
    int printer(char *, void *data), (*printer_p)( char *, void *) = printer;
    int strfree( void *d ), (*strfree_p)( void * ) = strfree;
    ..
    hash_enumerate( &table, printer_p);
    ..
    hash_free_table( &table, strfree_p )
    }
    ================================================
    Compilation unit that is being called, hash.o:

    void hash_free_table( hash_table *table, void (*func)(void *) )
    {
    ....
    }
    void hash_enumerate( hash_table *table, void (*func)(char *, void *) )
    {
    ....
    }
    void printer(char *string, void *data)
    {
    ....
    }
    void strfree( void *d )
    {
    ....
    }
    Andrew Slade, Dec 30, 2003
    #1
    1. Advertising

  2. Andrew Slade wrote:

    > Hello,
    >
    > I am making calls from one compilation unit to functions in another by
    > pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The
    > executable seems to work fine but the warnings bothers me a lot, mostly
    > because I don't understand what I did wrong. The code I used I got from the
    > FAQ on the matter, question 4.12.
    >
    > "warning: passing arg 2 of `hash_emumerate' from incompatible pointer type"
    > "warning: passing arg 2 of `hash_free_table' from incompatible pointer
    > type"
    >
    > Compilation unit that is calling, stub.o
    > {
    > ..
    > int printer(char *, void *data), (*printer_p)( char *, void *) = printer;
    > int strfree( void *d ), (*strfree_p)( void * ) = strfree;
    > ..
    > hash_enumerate( &table, printer_p);
    > ..
    > hash_free_table( &table, strfree_p )
    > }
    > ================================================
    > Compilation unit that is being called, hash.o:
    >
    > void hash_free_table( hash_table *table, void (*func)(void *) )
    > {
    > ....
    > }
    > void hash_enumerate( hash_table *table, void (*func)(char *, void *) )
    > {
    > ....
    > }
    > void printer(char *string, void *data)
    > {
    > ....
    > }
    > void strfree( void *d )
    > {
    > ....
    > }
    >
    >


    The hash_enumerate() function expects a pointer to a function returning
    void, but you provide a function returning int. Same thing for the
    hash_free_table() function.


    HTH
    Bjørn



    --
    The worlds fastest web server is now available
    at http://highlander.metasystems.no:2000. Enjoy!
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Dec 30, 2003
    #2
    1. Advertising

  3. Andrew Slade wrote:
    > Hello,
    >
    > I am making calls from one compilation unit to functions in another by
    > pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The
    > executable seems to work fine but the warnings bothers me a lot, mostly
    > because I don't understand what I did wrong. The code I used I got from the
    > FAQ on the matter, question 4.12.
    >
    > "warning: passing arg 2 of `hash_emumerate' from incompatible pointer type"
    > "warning: passing arg 2 of `hash_free_table' from incompatible pointer
    > type"


    The code you posted does not tell us what prototype the calling TU has for
    the called functions. However, ...

    > int printer(char *, void *data), (*printer_p)( char *, void *) = printer;

    [...]
    > hash_enumerate( &table, printer_p);


    printer_p returns an int here, while

    > void hash_free_table( hash_table *table, void (*func)(void *) )


    the second argument to hash_free_table and hash_enumerate returns void.

    This is a clear mismatch



    --
    Martin Ambuhl
    Martin Ambuhl, Dec 30, 2003
    #3
  4. Andrew Slade wrote:

    > Hello,
    >
    > I am making calls from one compilation unit to functions in another by
    > pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The
    > executable seems to work fine but the warnings bothers me a lot, mostly
    > because I don't understand what I did wrong. The code I used I got from the
    > FAQ on the matter, question 4.12.
    >
    > "warning: passing arg 2 of `hash_emumerate' from incompatible pointer type"
    > "warning: passing arg 2 of `hash_free_table' from incompatible pointer
    > type"
    >
    > Compilation unit that is calling, stub.o
    > {
    > .
    > int printer(char *, void *data), (*printer_p)( char *, void *) = printer;
    > int strfree( void *d ), (*strfree_p)( void * ) = strfree;


    Note the return 'int' return types.

    > .
    > hash_enumerate( &table, printer_p);
    > .
    > hash_free_table( &table, strfree_p )
    > }
    > ================================================
    > Compilation unit that is being called, hash.o:
    >
    > void hash_free_table( hash_table *table, void (*func)(void *) )


    Here the second argument is for a function pointer with a 'void' return
    type, not 'int'.

    > {
    > ...
    > }
    > void hash_enumerate( hash_table *table, void (*func)(char *, void *) )


    Ditto.

    > {
    > ...
    > }
    > void printer(char *string, void *data)


    Redeclaration with a different return type.

    > {
    > ...
    > }
    > void strfree( void *d )


    Ditto.

    Also, the identifier 'strfree' is reserved for future use by the
    standard library <string.h> header (as are all identifiers beginning
    with 'str', 'mem', or 'wcs' followed by a lower-case letter). You should
    probably choose a different name.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 30, 2003
    #4
  5. Kevin Goodsell wrote:

    > Andrew Slade wrote:
    >
    >> Hello,
    >>
    >> I am making calls from one compilation unit to functions in another by
    >> pointers and I get the warnings below from gcc on 2.4.x Debian
    >> Linux. The
    >> executable seems to work fine but the warnings bothers me a lot, mostly
    >> because I don't understand what I did wrong. The code I used I got
    >> from the
    >> FAQ on the matter, question 4.12.
    >>
    >> "warning: passing arg 2 of `hash_emumerate' from incompatible pointer
    >> type"
    >> "warning: passing arg 2 of `hash_free_table' from incompatible pointer
    >> type"
    >>
    >> Compilation unit that is calling, stub.o
    >> {
    >> .
    >> int printer(char *, void *data), (*printer_p)( char *, void *) = printer;
    >> int strfree( void *d ), (*strfree_p)( void * ) = strfree;

    >
    >
    > Note the return 'int' return types.
    >
    >> .
    >> hash_enumerate( &table, printer_p);
    >> .
    >> hash_free_table( &table, strfree_p )
    >> }
    >> ================================================
    >> Compilation unit that is being called, hash.o:
    >>
    >> void hash_free_table( hash_table *table, void (*func)(void *) )

    >
    >
    > Here the second argument is for a function pointer with a 'void' return
    > type, not 'int'.
    >
    >> {
    >> ...
    >> }
    >> void hash_enumerate( hash_table *table, void (*func)(char *, void *) )

    >
    >
    > Ditto.
    >
    >> {
    >> ...
    >> }
    >> void printer(char *string, void *data)

    >
    >
    > Redeclaration with a different return type.
    >
    >> {
    >> ...
    >> }
    >> void strfree( void *d )

    >
    >
    > Ditto.
    >
    > Also, the identifier 'strfree' is reserved for future use by the
    > standard library <string.h> header (as are all identifiers beginning
    > with 'str', 'mem', or 'wcs' followed by a lower-case letter). You should
    > probably choose a different name.
    >


    I accidentally posted this prematurely. I meant to add this link, for
    your information:

    http://www.oakroadsystems.com/tech/c-predef.htm

    This contains a list of reserved identifiers (in C and C++).

    (I also meant to spell-check before posting. I don't see any obvious
    spelling errors, but to me many spelling errors are non-obvious. ;) )

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 30, 2003
    #5
  6. Andrew Slade

    Andrew Slade Guest

    Thank you all for you answers. I had blindly followed the FAQ example that
    showed functions returning "int" and had decided the process involved
    casting all pointers to "int"s. Somehow, sort of...

    Anyway, thanks.
    Andrew Slade, Dec 31, 2003
    #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. Charlie
    Replies:
    6
    Views:
    486
  2. matt p
    Replies:
    3
    Views:
    469
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Nov 7, 2004
  3. Richard Bos

    calling other functions

    Richard Bos, Nov 14, 2003, in forum: C Programming
    Replies:
    11
    Views:
    524
    Florian Weingarten
    Nov 15, 2003
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    1,879
    Eric Sosman
    Jan 7, 2005
  5. Ted Sung
    Replies:
    1
    Views:
    307
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page