I am getting confused here in the code:
#include <iostream>
#include <ctime>
using namespace std;
double vals[] = { 10.1, 12.6, 33.1, 24.1, 50.0 };
double& setValues(int i)//if i remove the &operator, i get an error, why ?
{
return vals[i]; // return a reference to the ith element
}
int main()
{
cout << "Value before change" << endl;
for (int i = 0; i < 5; i++)
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
setValues(1) = 20.23; // change 2nd element
setValues(3) = 70.8; // change 4th element
cout << "Value after change" << endl;
for (int i = 0; i < 5; i++)
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
return 0;
}
If i remove the & operator in the function
double& setValues(int i);
i get the error as 'l-value required as the left operand of the assignment' Is that means the value vals[i] should be placed in the right? and with & operator this part becomes l-value ?