0

I'm trying to pass a 2D array to a function to initialize it. I can't use vectors for this project. 'unique' is an integer produced from reading in a file. I'm trying to pass my array as such:

void initialize(int adjMatrix[unique][unique])
{
  // initialize
}

void main()
{
    int adjMatrix[unique][unique];

    initialize(adjMatrix);
}

I am getting the following error when trying to compile with clang++:

"candidate function not viable: no known conversion from 'int [unique][unique]' to 'int (*)[unique]' for 1st argument void initialize(int adjMatrix[unique][unique]);"

Is there a better way for me to pass this 2D array? I have tried the suggestions at this link: Passing a 2D array to a C++ function

Passing these arrays in this manner works for me in Visual Studio 2019, but I am using Visual Studio Code and compiling with clang++ from Ubuntu now, could this be causing my issue? Thank you.

yepp
  • 41
  • 4
  • 1
    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) – Mesar ali Sep 19 '21 at 04:24
  • @crack_iT I tried all of the suggestions in the top rated comment there. – yepp Sep 19 '21 at 04:57
  • const int unique = 4; void initialize(int adjMatrix[][unique]) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < unique; ++j) { std::cout << adjMatrix[i][j] << ' '; } std::cout << std::endl; } } int main() { int adjMatrix[unique][unique] = {{3,3,2,12}, {3,33,22,42}, {6,4,3,2}, {33,32,32,2}}; initialize(adjMatrix); return 0; } – Mesar ali Sep 19 '21 at 05:22

0 Answers0