Search code examples
cgccgcc-warninggcc4

Why gcc4 warn and how to avoid it


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???


Solution

  • 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 .)