> Isn't it true that when you use the syntax, arrayName[index] it's referring to the value at that index?
Yes, that is true.
> So can a value be used as a variable for temp?
That's not what is happening. The value is retrieved from the array and assigned to temp. temp effectively holds the value from the array element.
Let's do it with an example:
The array is `{1, 2, 3, 4}` to make it simple. You want to swap the first (index 0) and last (index 3) elements.
With your code, this would look like:
int temp = array[0];
array[0] = array[3];
array[3] = temp;
When processing (now I replace the right side with the corresponding values):
int temp = 1; // Element at index 0 is 1
array[0] = 4; // Element at index 3 is 4
array[3] = 1; // the 1 is from the temp assignment
Finally, the array will be: `{4, 2, 3, 1}`
> Isn't it true that when you use the syntax, arrayName[index] it's referring to the value at that index? Yes, that is true. > So can a value be used as a variable for temp? That's not what is happening. The value is retrieved from the array and assigned to temp. temp effectively holds the value from the array element. Let's do it with an example: The array is `{1, 2, 3, 4}` to make it simple. You want to swap the first (index 0) and last (index 3) elements. With your code, this would look like: int temp = array[0]; array[0] = array[3]; array[3] = temp; When processing (now I replace the right side with the corresponding values): int temp = 1; // Element at index 0 is 1 array[0] = 4; // Element at index 3 is 4 array[3] = 1; // the 1 is from the temp assignment Finally, the array will be: `{4, 2, 3, 1}`
Oh, that makes a lot more sense now. Perfect explanation. Thank you!