I am trying to declare an array inside an if-statement. I wrote my code this way so that the object stays in scope once the if-block exits, but now I have a new issue: \"tak
You can use a temporary array to achieve this
int temp [] = {0,1,0,1,-4,1,0,1,0};
size_t n = sizeof(temp)/sizeof(int);
if (condition == true )
{
maskArray = new int[n]{0,1,0,1,-4,1,0,1,0};
}
// ...
delete [] maskArray; // Free memory after use
Or simply use a std::vector
std::vector maskArray;
if( condition == true )
{
maskArray = {0,1,0,1,-4,1,0,1,0}; // C++11 initializer_list vector assignment
}