+ 2
C++ array index printing
6 Respuestas
+ 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[0];
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[1] here, the second element, which is 2.
+ 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[1] and second index in arr are 2. So, arr[x] would be 2. Remember: 1 - index 0, 2 - index 1 and so on.
+ 4
Hello Josiah Mitchell, let's review the code.
int arr [] = {1,2,3,4};
int x = arr[0];
// At this point <x> contains value 1 (element at index 0)
cout << arr[x];
// Since <x> value is 1, arr[x] means arr[1], whose value is 2.
Hth, cmiiw
+ 3
int arr []={1,2,3,4};
int x=arr[0]; //arr[0]==1 so x == 1
cout << arr[x]; // arr[1] == 2 #
+ 3
Thank you all for taking time out of your day to answer my question.
+ 1
I tried printing the index 0 of my array, but the index to the right prints instead. Why does this happen?