0

I am trying to make some functions working on two dimensional arrays:

void display_matrix(int**, int, int);
void gen_matrix(int**, int, int);

int main()
{
    srand(time(0));
    int m=5, n=3;

    int my_matrix[m][n];
    gen_matrix(my_matrix, m, n);
    display_matrix(my_matrix, m, n);
}

I don't know what's wrong, but I get the following error when I call the functions: [Error] cannot convert 'int ()[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'int*' for argument '1' to 'void gen_matrix(int**, int, int)'

I know I can use vector but I am trying to practise and remember the use of pointers and arrays.

3
  • You could also consider using a template class with the dimensions as integral parameters. Commented Nov 21, 2013 at 22:00
  • 1
    int** is not a two dimensional array. So, here's your problem. Commented Nov 21, 2013 at 22:09
  • "I am trying to practise and remember the use of pointers and arrays" Then the way this code is written you should be compiling it as C, because at least through C++11 the language does not support variable-length arrays as you're using in main(). Some vendors support it by extension, but it is not part of the standard. And if you're using them in main is there some reason you decided not to use them in your parameter list? Commented Nov 21, 2013 at 22:17

2 Answers 2

1

Declaring a matrix in the form <type> <name>[<dim1>][<dim2>] defines a block of memory with an implicit stride of dim1. Internally elements are accessed by using multiples of dim1 to reach the correct row and offsetting from there by the second dimension.

the type <type> <name>** is a pointer to an array of pointers - very different. The structure consists of an array of pointers to rows of data. These have to be allocated and linked appropriately before calling the subroutine. There is also no requirement that they are contiguously allocated, and an indirect lookup needs to be done to each an element on each new row.

The advantage is that the rows can be different lengths suiting some algorithms that do not have rectangular structure.

Sign up to request clarification or add additional context in comments.

2 Comments

So should I just declare my_matrix as a int**, allocate it and then all its rows? int** my_matrix = (int**)malloc(nsizeof(int)); for (int i=0; i<n; i++) my_matrix[i] = (int*)malloc(m*sizeof(int)); Like that?
That's it. Then you can keep the original function signatures.
0

Change the code the following way

const int n = 3;

void display_matrix( int ( * )[n], int );
void gen_matrix( int ( * )[n], int);

int main()
{
    srand(time(0));
    const int m = 5;

    int my_matrix[m][n];
    gen_matrix( my_matrix, m );
    display_matrix(my_matrix, m );
}

Or you can keep your functions as defined but call them for example as

gen_matrix( reinterpret_cast<int **>( my_matrix ), m, n);

3 Comments

Why does n have to be constant? I couldn't set its value in the main function as I wished then.
Because it has to be part of the type. Two-dimensional array NxM is just a one-dimensional array containing N*M elements (row-by-row), but it has special type which points the compiler that double indexing is allowed. Compiler should know how many elements are there in any dimension except the first to calculate offset properly.
You can try using C11 standard or passing array and its dimension separately and then converting it to the corresponding type inside the function. That can help overpass the limit. You can also do the indexing by yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.