为什么 gcc4 发出警告以及如何避免它
我有一个函数声明为:
void event_add_card (EventAddr addr, EventType type, unsigned char card);
和 union
typedef union EventData
{
float money; /**< money info */
unsigned char card; /**< new card */
}
EventData;
当我编译以下代码时:
EventData data = {};
event_add_card (0,0, data.card);
启用警告 -Wconversion
我收到以下警告:
player-stud.c|71| warning: passing argument 3 of 'event_add_card' with different width due to prototype
为什么 gcc4 不成功以及如何修复它???
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 4.3 之前的 gcc 版本中,当行为可能因原型是否在范围内而有所不同时,
-Wconversion
会生成警告。在您给出的示例中,参数 3 (data.card
) 是unsigned char
;如果event_add_card()
的原型在范围内,它将作为unsigned char
传递,但如果原型不在范围内,它将作为传递>int
由于 C 的整数提升规则 - 因此出现警告。这不是很有用,除非在将旧的 K&R 风格代码转换为使用原型的过程中。因此,如果您使用的是旧版本的 gcc,那么启用它通常不是一个有用的选项。
从 gcc 4.3 开始,行为发生了变化:它现在警告任何可能更改值的隐式转换(例如,有符号和无符号整数之间,或整数和浮点数之间)。这相当有用。 (不过,以前的功能并没有消失:它仍然可用,但重命名为
-Wtraditional-conversion
。)(更多详细信息,请访问 http://gcc.gnu.org/wiki/NewWconversion 。)
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 ofunsigned char
; if the prototype forevent_add_card()
is in scope, it will be passed as anunsigned char
, but if the prototype is not in scope it will be passed as anint
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 .)
问题是您正在使用 -Wconversion
您的代码没有任何问题,并且您无法采取任何措施使 -Wconversion 不产生该警告。
示例:
如果您编译并使用 -Wconversion,这将产生相同的错误
-Wconversion 旨在识别 K&R 和 ISO C 之间转换时的问题,而您没有这样做。
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:
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.