Why is gcc4 warning and how to avoid it?
I have a function declared as:
void event_add_card (EventAddr addr, EventType type, unsigned char card);
and unification
typedef union EventData
{
float money; /**< money info */
unsigned char card; /**< new card */
}
EventData;
When I compile the following code:
EventData data = {};
event_add_card (0,0, data.card);
with warning enabled -Wconversion
I get the following warning:
player-stud.c|71| warning: passing argument 3 of 'event_add_card' with different width due to prototype
Why is gcc4 not successful and how to fix it ???
a source to share
Prior to gcc 4.3, -Wconversion
issues a warning when behavior may differ depending on whether the prototype is in scope. In the above example, argument 3 ( data.card
) is unsigned char
; if the prototype for event_add_card()
is in scope, it will be passed as unsigned char
, but if the prototype is not in scope, it will be passed as int
because of the C-integer promotion rules - hence the warning.
This is not very useful except for the process of converting old K&R style code to use prototypes. Thus, if you are using an older version of gcc, this is not a very useful option to include.
Since gcc 4.3, the behavior has changed: it now warns of any implicit conversion that might change the value (say between unsigned integers or integers or floats). This is much more useful. (The previous functionality hasn't gone away yet: it's still available, but renamed as -Wtraditional-conversion
.)
(See http://gcc.gnu.org/wiki/NewWconversion for details .)
a source to share
The problem is you are using -Wconversion
There is nothing wrong with the code, and there is nothing you can do to prevent -Wconversion from throwing this warning.
Example:
void someFunction(unsigned char foo);
...
unsigned char bar = 'f';
someFunction(bar);
This will produce the same error if you compile and use -Wconverersion
-Wconversion is for spotting conversion problems between K&R and ISO C that you don't.
a source to share