关于c ++:在{}中将’65280’从’int’转换为’short int’

Narrowing conversion of '65280' from 'int' to 'short int' inside { }

我有两个数组:

1
2
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};

编译时,出现以下消息:

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

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

另外,我总是会遇到访问冲突。
为什么? 我该如何解决?


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

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