0

I want to know how to fix error in returning array using pointer I have always this error please help me to fix this problem

error: cannot convert 'char (*)[n]' to 'char**' in return| .

char **nextstate(char arr[][5], int n)// error line code
{ char next[n][n];
    for (int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {if(emptyorstay(arr,i,j)==true)next[i][j]= '1';
           else next[i][j]= '0';
        }
    }
    return next;

  • 3
    Your bigger problem is that you are trying to return a pointer to a local object that is going to go away when the function returns. – jkb Jun 10 '21 at 23:53
  • As seen in the error message `char next[n][n]` will [decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) to `char *[n]`, not to `char **`. The top-voted answer [to this question](https://stackoverflow.com/questions/8617683/return-a-2d-array-from-a-function) is "horrible", but it, or something similar, is what's asked for as soon as an assignment requires a return type of `char **`. Better approaches use `std::vector`, either with a `vector>` or [something like this](https://stackoverflow.com/a/2076668/4581301) to take full advantage of cache. – user4581301 Jun 11 '21 at 00:32

0 Answers0