Warnings calling functions in other object files by pointer

A

Andrew Slade

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 )
{
....
}
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Andrew said:
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
 
M

Martin Ambuhl

Andrew said:
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
 
K

Kevin Goodsell

Andrew said:
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
 
K

Kevin Goodsell

Kevin said:
Note the return 'int' return types.



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



Redeclaration with a different return type.



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
 
A

Andrew Slade

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top