为什么 gcc4 发出警告以及如何避免它

发布于 2024-08-29 05:39:12 字数 616 浏览 12 评论 0原文

我有一个函数声明为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-09-05 05:39:12

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

终难愈 2024-09-05 05:39:12

问题是您正在使用 -Wconversion

您的代码没有任何问题,并且您无法采取任何措施使 -Wconversion 不产生该警告。

示例:

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

如果您编译并使用 -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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文