+ 2

# C++ array index printing

https://code.sololearn.com/coTtk4MVUz9F/?ref=app

27th May 2019, 8:41 PM
Josiah Mitchell + 6
int arr [] = {1,2,3,4}; This puts numbers into your array at index 0 to 3 (arrays start counting at 0). int x = arr; This stores the number in x that is at index 0 of that array. So x is 1 now. cout << arr[x]; You are accessing arr here, the second element, which is 2.
27th May 2019, 9:02 PM
HonFu + 5
First of all, index of elements starts from 0 in array. If you set vatiable x to ELEMENT (in other case, that was number 1), expected is that cout << arr[x]; became arr and second index in arr are 2. So, arr[x] would be 2. Remember: 1 - index 0, 2 - index 1 and so on.
27th May 2019, 10:01 PM
Aleksandar Nedeljkovic + 4
Hello Josiah Mitchell, let's review the code. int arr [] = {1,2,3,4}; int x = arr; // At this point <x> contains value 1 (element at index 0) cout << arr[x]; // Since <x> value is 1, arr[x] means arr, whose value is 2. Hth, cmiiw
27th May 2019, 8:59 PM
Ipang
+ 3
int arr []={1,2,3,4}; int x=arr; //arr==1 so x == 1 cout << arr[x]; // arr == 2 #
27th May 2019, 9:21 PM
Basel.Al_hajeri?.MBH() + 3
Thank you all for taking time out of your day to answer my question.
27th May 2019, 9:33 PM
Josiah Mitchell + 1
I tried printing the index 0 of my array, but the index to the right prints instead. Why does this happen?
27th May 2019, 8:45 PM
Josiah Mitchell 