2

I wanna pass 2D char array to function.

However, an error comes:

Cannot convert 'char(*)[50]' to 'char**' for argument '1' to 'void prac(char**)'

How can I fix the code ? Please help me.

Code:

#include <iostream>
using namespace std;

void prac(char **b)
{
        for(int i=0;i<50;i++)
        {
                for(int j=0;j<50;j++)
                {
                        cout << b[i][j] << " ";
                }
                cout << endl;
        }
}

int main()
{
        char a[50][50];
        for(int i=0;i<50;i++)
        { 
                for(int j=0;j<50;j++)
                {
                        cin >> a[i][j];
                }
        }

        prac(a); // error position

        return 0;
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
appleJuice
  • 137
  • 2
  • 4
  • 9

1 Answers1

4

The name of a two-dimensional arrays gets converted ("decay") to a pointer to the address of the first row of the array. So use

void prac(char b[][50])

or

void prac(char (*b)[50])

instead of

void prac(char **b)

Your compiler complained because a cannot be converted to a char**.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83