0

I was learning c++ and implementing the game of life when I created a helper function to print the current board which is a 2d array. I cannot seem to pass the array into the function as I get an error, "Candidate function not viable: no known conversion from 'char [rows][cols]' to 'char (*)[cols]' for 3rd argument." I am using Xcode as an ide if that helps.

void printArray(int rows, int cols, char board[rows][cols]){
    for(int r = 0; r < rows; r++){
        for(int c = 0; c < cols; c++){
            cout << board[r][c];
            cout << " ";
        }
        cout << "\n";
    }
}

int main(){
     char board[5][5];
    for(int r = 0; r < 5; r++){
        for(int c = 0; c < 5; c++){
            board[r][c] = 0;
        }
    }
    printArray(5, 5, board);
    return 0;
}

I've tried switching up the parameter to different things such as char **board, char board[][cols], char (*board)[cols]. Even casting my input board which leads to other errors.

  • Does this answer your question? [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – J. Doe Dec 30 '19 at 05:13
  • You should avoid using built-in arrays in the first place. If you used `std::vector` you would not have this problem. – walnut Dec 30 '19 at 05:27
  • @J.Doe I already tried that didn't work – Omar Dadabhoy Dec 30 '19 at 05:29
  • @walnut yea I realize my mistake now but I thought I might as well figure this out to Rest In Peace. – Omar Dadabhoy Dec 30 '19 at 05:29
  • The trick you're using here `void printArray(int rows, int cols, char board[rows][cols])` works in C and possibly by extension in C++, but to be fully compliant in C++ all array dimensions must be constant. – user4581301 Dec 30 '19 at 05:51

3 Answers3

2

If you want to pass 2d arrays to a function there is a special syntax. Unfortunately, the other previous 2 answers do not answer fully correctly.

You can pass by reference or by pointer. The array dimensions must be compile time constants. That is a requirement from C++.

Please see:

constexpr size_t NumberOfRows = 3;
constexpr size_t NumberOfColumns = 4;

// Typedef for easier usage
using IntMatrix2d = int[NumberOfRows][NumberOfColumns];

//Solution 1 ------
// Pass by reference
void function1(int(&matrix)[NumberOfRows][NumberOfColumns])  {}

// Pass by pointer
void function2(int(*m)[NumberOfRows][NumberOfColumns])  {}

//Solution 2 ------
// Pass by reference
void function3(IntMatrix2d& matrix) {}

// Pass by pointer 
void function4(IntMatrix2d* matrix) {}


int main()
{
    // Solution 1
    // Handwritten matrix. Dimension is compile time constant
    int matrix1[NumberOfRows][NumberOfColumns];

    // Pass by reference
    function1(matrix1);

    // Pass by pointer
    function2(&matrix1);

    // Solution 2 -----
    IntMatrix2d matrix2;

    // Pass by reference
    function3(matrix2);

    // Pass by pointer
    function4(&matrix2);

    return 0;
}

If you typedef or use using for your type definition, then it gets rather intuitive.

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
1

If you are not very comfortable with pointers then there are some easy ways to do the task

1. You have to define the 2d array size by default, before passing array to the function so that the size doesn't seem to be unknown to the function.

#include <iostream>

const std::size_t rows=5;
const std::size_t cols=5;

void printArray(char board[rows][cols]) {
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            std::cout << board[r][c];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    char board[rows][cols];
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            board[r][c] = '0';
        }
    }
    printArray(board);
    return 0;
}


2. Use vector. Make your board a vector.

#include <iostream>
#include <vector>
void printArray(std::vector<std::vector<char>> &board) {
    for (int r = 0; r < board.size(); r++) {
        for (int c = 0; c < board[0].size(); c++) {
            std::cout << board[r][c];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    std::vector<std::vector<char>> board(rows, std::vector<char>(cols, '0'));
    printArray(board);
}
shubhgkr
  • 361
  • 1
  • 6
0

I encountered this problem while doing a project for a class. To work around it, I made a double pointer array, and then used passed it to the function to manipulate it.

int** createArr(){

    int** pixels = 0;
    pixels = new int*[numrows];

    for (int row = 0; row < numrows; row++){
    pixels[row] = new int[numcols];

            for (int col = 0; col < numcols; col++){
                ss >> pixels[row][col];

            }
    }
    return pixels;
}

int** newArr = createArr(); //calls function to create array

func(newArr); //where func is a function that modifies the array.

Don't forget to delete your arrays at the end to avoid memory leaks. Hope this helps.

Aubrey Champagne
  • 125
  • 1
  • 10
  • To complete this answer you should add the destruction logic as correctly freeing all of the memory from an array of arrays is non-trivial. – user4581301 Dec 30 '19 at 06:38
  • A Solution should not introduce more problems than it solves. `new[]` in user code ... really!?? – J. Doe Dec 30 '19 at 11:45