views:

123

answers:

2

I have a function declared as:

void    event_add_card (EventAddr addr, EventType type, unsigned char card);

and union

typedef union EventData
{
    float           money;      /**< money info */
    unsigned char   card;       /**< new card */
}
EventData;

When i compile following code:

EventData data = {};

event_add_card (0,0, data.card);

with enabled warning -Wconversion I receive following warning:

player-stud.c|71| warning: passing argument 3 of 'event_add_card' with different width due to prototype

Why gcc4 unsuccessful and how to fix It???

+1  A: 

The problem is that you're using -Wconversion

There is nothing wrong with your code, and nothing you can do to make -Wconversion not produce that warning.

Example:

void someFunction(unsigned char foo);
...
unsigned char bar = 'f';
someFunction(bar);

This will produce the same error if you compile and use -Wconversion

-Wconversion is meant to identify issues when converting between K&R and ISO C, which you aren't doing.

Brian Roach
+2  A: 

In versions of gcc prior to 4.3, -Wconversion produces a warning when the behaviour may be different depending on whether or not a prototype is in scope. In the example you give, argument 3 (data.card) is of unsigned char; if the prototype for event_add_card() is in scope, it will be passed as an unsigned char, but if the prototype is not in scope it will be passed as an int due to C's integer promotion rules - hence the warning.

This isn't very useful, except during the process of converting old K&R-style code to use prototypes. As such, if you're using an older version of gcc, it's not a generally useful option to enable.

From gcc 4.3 onwards, the behaviour has changed: it now warns about any implicit conversion which might change a value (say, between signed and unsigned integers, or integers and floats). This is considerably more useful. (The previous functionality hasn't gone, though: it's still available, but renamed as -Wtraditional-conversion.)

(More details at http://gcc.gnu.org/wiki/NewWconversion .)

Matthew Slattery
*THANKS*, it's good note
vitaly.v.ch