I am trying to return array of strings from a function and loop throught it:
string* fetchArray()
{
string myArray[10] = { "0" };
myArray[9] = { "End" };
return myArray;
};
int main()
{
string* fetchedArray = fetchArray();
while (*fetchedArray != "End")
{
cout << *fetchedArray << endl;
fetchedArray++;
}
return 0;
}
However, I am doing something wrong. This results in runtime error. I can see while I debug that the *fetchedArray is empty after function call, which could mean that the function did not return what I was expecting. I was at least expecting that the first element of the array will be correct.
Error:
Exception thrown at 0x54AA40D5 (vcruntime140d.dll) in Test.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
Usually, I would use vector in this situation, but I wanted to test this behaviour.