+ 2
Array name points to first index of array
#include <iostream> using namespace std; int main() { int* array = new int[5]; for(int i=0;i<5;++i) array[i] =i+2; array +=2; cout<< array[1]; return 0; } Refer above code...output here is 5. I am unable to understand this... for loop initialize all array element with index plus 2. with this, array[1] becomes 3, but output is 5 as verified on compiler..... Does array+=2 is tricky? As far as I know, array name points to first element of array I.e. array[0], not at all array[1]. Why array[1] is getting updated by this line?....so, output should be 3, not 5....
1 ответ
+ 2
array[0] is moved by the +=2 to array[2] so array[3] is outputted by cout.