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?

8
  • 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). Commented Jul 25, 2013 at 15:35
  • 1
    You need to read a basic C language guide. Commented Jul 25, 2013 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. Commented Jul 25, 2013 at 15:35
  • 1
    Four ways to pass 2-D matrix in c/c++ Commented Jul 25, 2013 at 15:36
  • 1
    Also read this. Commented Jul 25, 2013 at 15:42

1 Answer 1

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.

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

2 Comments

void test(double (&matrix)[2][2]) gives error: expected ‘)’ before ‘&’ token (at least on C) is for C++?
@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].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.