在{}内缩小'65280'从'int'到'short int'的转换 [英] Narrowing conversion of '65280' from 'int' to 'short int' inside { }

查看:135
本文介绍了在{}内缩小'65280'从'int'到'short int'的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

short GMobiles[18] = {0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x00FF, 0xFFFF};
short GMobiles2[18] = {0xFF00, 0xFF01, 0xFF02, 0xFF03, 0xFF04, 0xFF05, 0xFF06, 0xFF07, 0xFF08, 0xFF09, 0xFF0A, 0xFF0B, 0xFF0C, 0xFF0D, 0xFF0E, 0xFF0F, 0xFFFF, 0xFFFF};

当我编译时,我收到以下消息:

When I compile, I get the following message:


[警告]缩小'65280'从'int'到'short int'的转换
{} [-Wnarrowing]

[Warning] narrowing conversion of '65280' from 'int' to 'short int' inside { } [-Wnarrowing]

包含FF的每个元素,如0xFF0D或0xFFFF

for each element that contains FF, like 0xFF0D or 0xFFFF

此外,我总是遇到访问冲突。
为什么?我该怎么办?

Also, I always get access violation. Why? How could I fix that?

推荐答案

在你的系统上 short int的范围可能是 [ - 32768,+ 32767] int 65280 超出范围,因此存储在数组中的值是实现定义的。

On your system the range for short int is probably [-32768, +32767]. The int value 65280 falls outside of the range, and as a result the value stored in your array is implementation-defined.

如评论中所述,最好的解决方案可能是将类型更改为 unsigned short 范围 [0,65535]

As mentioned in comments, probably the best solution is to change the type to unsigned short which will have the range [0, 65535] .

这篇关于在{}内缩小'65280'从'int'到'short int'的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆