1
votes

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
Looks like you want unsigned short. - Mysticial
your snippet of code is not helpful. There is no conversion going on between int and short int in your code. This snippet of code aslo doesn't give me any error when I run it - Irrational Person
@IrrationalPerson: actually, there is one for each of the literals above 0x7FFF. - Matteo Italia
That's because (signed) short only ranges between [-32,768, 32,767]. Converting any value above that will be a narrowing conversion. - Beko
@MatteoItalia: All the integer literals are of type int, and they're all implicitly converted to short. - Keith Thompson

1 Answers

4
votes

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] .