问题
I have two arrays:
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:
[Warning] narrowing conversion of '65280' from 'int' to 'short int' inside { } [-Wnarrowing]
for each element that contains FF, like 0xFF0D or 0xFFFF
Also, I always get access violation. Why? How could I fix that?
回答1:
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.
As mentioned in comments, probably the best solution is to change the type to unsigned short
which will have the range [0, 65535]
.
来源:https://stackoverflow.com/questions/28389750/narrowing-conversion-of-65280-from-int-to-short-int-inside