jeudi 25 février 2021

Unable to pass 2D character array to function(C++)

I am trying to pass a 2-D character array to a function however vs code gives me the following error message:

cannot convert 'char ()[3]' to 'char ()[10]'gcc

Here is the code:

#include<string>
using namespace std;
void NodeDetect(char grid[][3], int height, int width)
{
    cout << "\nThe grid output:\n";
    for(int i = 0; i < height; ++i)
    {
        for(int j = 0; j < width; ++j)
            if(grid[i][j] == '0')
            {
                cout << '\n' <<  i << '\t' << j << ", ";

                if(grid[i][j + 1] == '0' && (j + 1) < width)//right neighbour
                    cout << i << '\t' << (j + 1) << ", ";
                else if(grid[i][j + 1] == '.' || (j + 1) == width)
                    cout << "-1 -1, ";

                if(grid[i + 1][j] == '0' && (i + 1) < height)//bottom neighbour
                    cout << (i + 1) << '\t' << j << ", ";
                else if(grid[i + 1][j] == '.' || (i + 1) == height)
                    cout << "-1 -1";
            }
            cout << '\n';
    }
}
int main()
{
    string line;
    char grid[3][3];
    int height, width;                          //height = rows
    cout << "Enter the height and the width:\t";//width = columns
    cin >> height >> width;
    cout << "\nEnter the strings:\n";
    for(int i = 0; i < height; ++i)//initializing the grid
        cin >> grid[i];

    /*
    cout << "\nThe grid:\n";
    for(int i = 0; i < height; ++i)     //displaying the grid
    {
        for(int j = 0; j < width; ++j)
            cout << grid[i][j] << '\t';
        cout << '\n';
    }
    */
    NodeDetect(grid, height, width);
    return 0;
}

I am trying to pass the 2D array grid to the function NodeDetect

Aucun commentaire:

Enregistrer un commentaire