int letters[] = {['A'] = 4, ['B'] = 8, ['E'] = 3, ['I'] = 1, ['O'] = 0, ['S'] = 5};
When I initialize the array letters above, my assumption is that at the index of each capital letter, the value will be the number i.e. if ['A'] = 4
, then at index 'A'
the value will be 4 and the rest of the values not initialized will be 0 by default.
But when I print all the values of the array letters, I am getting this output:
00000000000000000000000000000000000000000000000000000000000000000480030001000000000514303876720941309621-1392458268143038767232767-197939865932767-1979398659327670010143038792832767
I have no idea where the negative numbers are coming from.
'S'
is the highest index you gave a value for and, as your encoding's apparently ASCII, the length of your array is 83 (= 0x53). Everything with smaller indexes (without an initializer) is initialized to 0
.
When you print your array, you are accessing the array out-of-bounds, which is undefined behavior.
What your program probably outputs are the values which happen to be on the stack after your array. However, as said above, there is no guarantee about what will happen.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With