Reputation: 319
I'm just using a simple code that uses auto:
double **PArrays = new double*[3];
count = 0;
for(auto Array: PArrays)
{
Array = new double[6];
for(int i{}; i < 6; i++)
{
if(count == 0)
{
Array[i] = i;
std::cout<<"The value of Array i is: "<<Array[i]<<std::endl;
std::cout<<"The value of PArray is: "<<PArrays[count][i];
}
else if(count == 1)
{
Array[i] = i * i;
}
else
{
Array[i] = i * i * i;
}
}
count += 1;
}
I can't figure out why the values in the PArray[i][j], given that [i][j] are within bounds, results in the value of zero.
Furthermore, the compiler complains, it says that 'begin' was not declared in scope and then points to Array auto variable in the for loop, also, points to the same variable saying 'end' was not declared. :
for(auto Array: PArrays)
{
for(auto x: Array)
{
std::cout<<"The value is: "<<x;
}
std::cout<<std::endl;
}
Upvotes: 3
Views: 257
Reputation: 234885
for(auto Array: PArrays)
gives you a value copy of every element in PArrays
. So any changes you make in Array
will not be reflected in the original container PArrays
.
If you want Array
to be a reference to an element of PArrays
, then use
for(auto& Array: PArrays)
instead.
Upvotes: 8