-1

Here is my code:

void printArray(int **A, int m, int n)
{
    int row = m;
    int col = n;

    int **array;
    array = new int *[row];

    for (int i = 0; i < row; ++i)
    {
        array[i] = new int[col];
    }

    for (int p = 0; p < row; p++)
    {
        for (int l = 0; l < col; l++)
        {
            cout << array[row][col] << " ";
        }
    }
}
int main()
{

    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    cout << printArray(arr, 3, 3); // error:
    // invalid conversion from 'int' to 'int**' [-fpermissive]|
} 

Can anyone let me know what I'm doing wrong in the function call?

pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    I do not see where the pointer A is used in the function. – Vlad from Moscow Sep 18 '20 at 11:15
  • please include the complete error message in the question. It should contain more information, maybe you cannot decipher it, but error messages exist to help you find what is wrong in the code – 463035818_is_not_an_ai Sep 18 '20 at 11:20
  • Im not sure how to link `**A` to `**array` and then use call the function correctly in the main. - Thanks Vlad – Thomas Finucane Sep 18 '20 at 11:20
  • Error:||=== Build file: "no target" in "no project" (compiler: unknown) ===| C:\Users\tf\Documents\Data Structures and Algorithms in c++\C++ Book\R-1.9.cpp||In function 'int main()':| C:\Users\tf\Documents\Data Structures and Algorithms in c++\C++ Book\R-1.9.cpp|42|error: cannot convert 'int (*)[3]' to 'int**'| C:\Users\tf\Documents\Data Structures and Algorithms in c++\C++ Book\R-1.9.cpp|4|note: initializing argument 1 of 'void printArray(int**, int, int)'| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===| – Thomas Finucane Sep 18 '20 at 11:23

2 Answers2

2

For starters the function printArray declared like

void printArray(int **A, int m, int n);

does not make sense because the pointer A is not used within the function.

Moreover the parameter has the type int ** while the passed argument in this call

printArray(arr, 3, 3);

has the type int ( * )[3].

That is the pointer types are not compatible and as a result the compiler issues an error.

And there is no overloaded operator << for the incomplete type void that is the return type of your function.

To output any two-dimensional array (for example with elements of the type int though the element type also could be a type template parameter) you could write a template function the following way

template <size_t M, size_t N>
std::ostream & printArray( const int ( &a )[M][N], std::ostream &os = std::cout )
{
    for ( const auto &row : a )
    {
        for ( const auto &item : row )
        {
            os << item << ' ';
        }

        os << '\n';
    }

    return os;
}

And the function can be called like

printArray( arr ) << '\n';

Here is a demonstrative program.

#include <iostream>

template <size_t M, size_t N>
std::ostream & printArray( const int ( &a )[M][N], std::ostream &os = std::cout )
{
    for ( const auto &row : a )
    {
        for ( const auto &item : row )
        {
            os << item << ' ';
        }

        os << '\n';
    }

    return os;
}

int main() 
{
    int arr[3][3] = 
    {
        {1, 2, 3}, 
        {4, 5, 6}, 
        {7, 8, 9}
    };
    
    printArray( arr ) << '\n';
    
    return 0;
}

Its output is

1 2 3 
4 5 6 
7 8 9 

Otherwise without using a template you can declare and define the function the following way as it is shown in the demonstrative program below.

#include <iostream>

const size_t N = 3;

std::ostream & printArray( const int a[][N], size_t m, std::ostream &os = std::cout )
{
    for ( size_t i = 0; i < m; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            os << a[i][j] << ' ';
        }

        os << '\n';
    }

    return os;
}

int main() 
{
    int arr[][N] = 
    {
        {1, 2, 3}, 
        {4, 5, 6}, 
        {7, 8, 9}
    };
    
    const size_t M = sizeof( arr ) / sizeof( *arr );
    
    printArray( arr, M ) << '\n';
    
    return 0;
}

Again the program output is

1 2 3 
4 5 6 
7 8 9

Alternatively the function can be defined also the following way using the range-based for loop as the inner loop.

std::ostream & printArray( const int a[][N], size_t m, std::ostream &os = std::cout )
{
    for ( size_t i = 0; i < m; i++ )
    {
        for ( const auto &item : a[i] )
        {
            os << item << ' ';
        }

        os << '\n';
    }

    return os;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-2

By default, arrays are passed by pointer, whereas integers are passed by value. You can simply pass the array and the col and row into the function. Also, remember that instead of cout << array[row][col] << " ";, you should use cout << array[i][j] << " "; instead (or another variable if you wish). Below is an example. Another problem is that instead of cout << printArray(arr, 3, 3);, you should simply call printArray(arr, 3, 3);.

#include<iostream>
void printArray(int array[][something], int col, int row) {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            std::cout << array[i][j] << " ";
        }
        std::cout << '\n';
    }
}
int main()
{

    int arr[something][something] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    printArray(arr, 3, 3);
} 

Replace something with the actual size of your array

ITO
  • 156
  • 6
  • This wold not work becuase you are not allowed to have type like `int [][]` you can only have one unbound index, i.e. you can have `int [][3]`. – sklott Sep 18 '20 at 11:30