0

I created my test function: void test(double** matrix);

I want to pass to this function variable as double matrix[2][2] = {{1,2},{2,3}};. But my evil compiler writes: cannot convert «double (*)[2]» to «double**» for argument «1» to «void test(double**)».

What do I need to do?

StephenTG
  • 2,579
  • 6
  • 26
  • 36
  • 3
    Comply with the evil compiler's order. You can't substitute a `double[][]` to a `double**` because the memory layouts are different. You'll need to allocate your rows with `new` or find another way (hint: `vector`). – Nbr44 Jul 25 '13 at 15:35
  • 1
    You need to read a basic C language guide. –  Jul 25 '13 at 15:35
  • Mutidemensional arrays != jagged arrays. Jagged arrays are double** and must be allocated with new or malloc, and are an array of pointers. Multidimensional arrays are one continuous memory block that can be indexed funny. – IdeaHat Jul 25 '13 at 15:35
  • 1
    **Four** ways to [pass 2-D matrix in c/c++](http://stackoverflow.com/questions/17566661/c-dynamic-array-initalization-with-declaration/17567663#17567663) – Grijesh Chauhan Jul 25 '13 at 15:36
  • possible duplicate of [Declaring a pointer to multidimensional array: C++](http://stackoverflow.com/questions/3904224/declaring-a-pointer-to-multidimensional-array-c) –  Jul 25 '13 at 15:36
  • Thank for answers! It was strange for me, because when I do this with one-dimensional arrays I have not problems. Closed. – Febernya Febernya Jul 25 '13 at 15:40
  • 1
    [Also read this.](http://c-faq.com/aryptr/aryptr2.html) –  Jul 25 '13 at 15:42
  • possible duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function) – RiaD Jul 27 '13 at 21:26

1 Answers1

0

The types of the arguments and the parameters have to agree (or at least be compatible). Here, you have a double[2][2], and you want to pass it to a double**. The types are unrelated: an array of arrays is not an array of pointers (which would convert to a pointer to a pointer).

If you really want to pass a double [2][2], you'll have to declare the parameter as a double (*matrix)[2], or (better yet), a double (&matrix)[2][2].

Of course, if you're using C++, you'll want to define a Matrix class, and use it.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • `void test(double (&matrix)[2][2])` gives `error: expected ‘)’ before ‘&’ token` (at least on C) is for C++? – David Ranieri Jul 25 '13 at 15:58
  • @DavidRF Yes. It's purely C++. I hadn't noticed that the question was tagged both---the answer in C has to be `double (*matrix)[2]`. – James Kanze Jul 25 '13 at 16:34