Array name points to first index of array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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....

24th Jun 2018, 10:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
array[0] is moved by the +=2 to array[2] so array[3] is outputted by cout.
24th Jun 2018, 10:33 PM
John Wells
John Wells - avatar