0

Consider a function that returns a bool array. In the call statement of this function, how do you store the bool array in another bool array?

I can not figure out the syntax. For example, consider this function

void checkoutput(vector<vector<int>>& A, bool op[])
{
   int i;
   int n = A.size();
   bool actualop[n];
   actualop = checkiftriangleexists(A);
   for (i = 0; i < n; i++)
   {
      if (actualop[i] == op[i])
      {
         cout << "Pass" << endl;
      }
      else cout << "Fail" << endl;
   }
}

This function calls checkiftriangleexists(A) that returns a bool array. I want to store this into another bool array called actualop[].

Please help me figure out where I am going wrong.

JeJo
  • 30,635
  • 6
  • 49
  • 88
monkikat
  • 33
  • 1
  • 4

1 Answers1

0

As mentioned in https://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm you could use static keyword, otherwise the memory assigned to the internal array return by checkiftriangleexists, would be cleaned up after the code is done and is now out of the method zone.

But I prefer to use the memory assigned in the checkoutput function and pass it by reference to checkiftriangleexists and do the job. In this way you don't need to concern about the memory address being cleaned up in checkiftriangleexists method zone. List this:

void checkiftriangleexists(vector<vector<int>>&, bool[]);

void checkoutput(vector<vector<int>> &A, bool op[])
{
  ...
  bool actualop[n];
  checkiftriangleexists(A, actualop);
  ...
}