Subscript C / C ++ array may be negative

Today came across C / C ++ array index can be negative, was very strange. Usually write the code, subscript are starting from scratch, never take this into consideration. Write test code a bit, actually passed. But value is a mess. But if you write down the underlying value in the range of the array, it can output a correct value.

A int [. 5] = {1,2,3,4,5};
 int * PTr1 = (int *) (A + &. 1);
 
  the printf ( "% X, X%,", PTr1 [-1]);
1
2
3
4
the result is five;
later checked the Internet a bit, thought a moment, I feel actually quite normal. Subscript of an array head pointer array is actually moving, in C / C ++ compiler is no array bounds checking. For example above, you output a [6] is not wrong, but the value of a mess of it.
In fact, this feature in the development of almost less than, reason is that C / C ++ compiler lazy. In Java or the OC, they are being given.


 

Published 351 original articles · won praise 52 · views 190 000 +

Guess you like

Origin blog.csdn.net/u014644594/article/details/104008785