-2

I am getting an error when calling the 'printMat' function. My requirement is to create a matrix after accepting the number of rows and columns, and then take input into the matrix, then call the printMat by sending the matrix and print the elements. The error is as follows:

error: parameter 'a' includes pointer to array of unknown bo und 'int []'

#include<iostream>
using namespace std;

int row,col;

void printMat(int* a[])
{
    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<col; ++j)
        {
            cout<<a[i][j]<<" ";
        }
    }
}

int main()
{   
    cin>>row;
    cin>>col;

    int mat[row][col];

    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<col; ++j)
        {
            cin>>mat[i][j];
        }
    }

    printMat(mat);

    return 0;
}
Srinivasan A
  • 51
  • 3
  • 12
  • What error are you getting? – bpgeck Aug 07 '15 at 17:55
  • You should be getting an error at `int mat[row][col];`… C++ has no variable-length arrays. Try turning on `-pedantic-errors`. – Emil Laine Aug 07 '15 at 17:56
  • You can't declare a local array with a size unknown on compile time. Use `std::vector`. – user123 Aug 07 '15 at 17:56
  • error: cannot convert 'int (*)[(((unsigned int)(((int)col + -0x000000001)) + 1)]' to 'int**' for argument '1' to 'void printMat(int**)' – Srinivasan A Aug 07 '15 at 17:57
  • 1
    @SrinivasanA Add such information by [editing](http://stackoverflow.com/posts/31883892/edit) your question please. It's not useful in comments. – πάντα ῥεῖ Aug 07 '15 at 18:01
  • 1
    The tag to "c++" for this question has to be removed. There is nothing of c++ here except using cin and cout. Or start using std::vector at least. – StahlRat Aug 07 '15 at 19:25

3 Answers3

3
int* a[]

is an array of pointer, but you are passing a pointer to an array:

int (*a)[]
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    When i try int (*a)[], it gives the error: parameter 'a' includes pointer to array of unknown bound 'int []' – Srinivasan A Aug 07 '15 at 18:01
  • 2
    @SrinivasanA Ah, that's because you are using C++, and using a non-standard extension ([variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array)). If you want to continue programming in C++ I suggest you read [some good books or tutorials](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learn about [the standard containers](http://en.cppreference.com/w/cpp/container) like for example [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) which will help you a lot. – Some programmer dude Aug 07 '15 at 18:36
  • I'm tempted to ask on meta.stackoverflow.com about whether the community should organize a concerted and organized effort to discourage arrays and encourage vectors. (Yes, they're not always absolutely perfect for everyone, but they are essentially perfect for beginners). If we gave every student a clear and consistent message, that might eventually feed back to educators. – Aaron McDaid Aug 07 '15 at 19:42
1

The reason it doesn't work is that arrays are just a nightmare. In C++, we use vectors instead.

#include<iostream>
#include<vector>
using namespace std;


void printMat(vector<vector<int>> mat)
{
    for(vector<int> one_row : mat)
    {
        for(int one_cell_in_this_row : one_row)
        {
            cout << one_cell_in_this_row << ' ';
        }   
        cout << endl;
    }   
}   

int main()
{   
    int row,col;
    cin>>row;
    cin>>col;

    vector< vector<int> >   mat( row , vector<int>(col,0) );
    //                            ^                 ^
    // initialize the vector ~~~~~/                 |
    // with 'row' items, each                       |
    // of which is a vector                         |
    // of 'col' integers.  ~~~~~~~~~~~~~~~~~~~~~~~~~/

    for(int i=0; i<row; ++i)                 
    {                                        
        for(int j=0; j<col; ++j)             
        {                                    
            int current_entry;
            cin>>current_entry;
            mat.at(i).at(j) = current_entry;
        }
    }

    printMat(mat);

    return 0;
}
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
-1

You can solve using pointer arithmetic. See following code.

#include<iostream>
using namespace std;

int row,col;

void printMat(int *a)
{
    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<col; ++j)
        {
            cout<< *((a+i*col) + j)<<" ";
        }
    }
}

int main()
{
    cin>>row;
    cin>>col;

    int mat[row][col];

    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<col; ++j)
        {
            cin>>mat[i][j];
        }
    }

    printMat((int *)mat);

    return 0;
}

Other possible solutions are explained in this link

Francesco Argese
  • 626
  • 4
  • 11