Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Warnings calling functions in other object files by pointer

Reply
Thread Tools

Warnings calling functions in other object files by pointer

 
 
Andrew Slade
Guest
Posts: n/a
 
      12-30-2003
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 )
{
....
}


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Guest
Posts: n/a
 
      12-30-2003
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!
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-30-2003
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

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-30-2003
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.
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-30-2003
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.
 
Reply With Quote
 
Andrew Slade
Guest
Posts: n/a
 
      12-31-2003
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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
Calling a object's overloaded operator with a pointer to that object. matt p C++ 3 11-07-2004 11:09 PM
Calling functions in other files... Charlie C++ 6 10-06-2004 06:14 PM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments