1

So code is for matrix multiplication

#include <iostream>

int FibbonaciOperator[2][2] = {
    1, 0,
    0, 1};

int FibbonaciStarter[2] = {
    0, 1};

void multiply(int (&Matrix)[2][2], int (&Matrix2)[][2]) // int(&Matrix2)[2][2]
{

   int MatrixRez[2][2] = {
       0, 0, 0, 0};

   for (int i = 0; i < 2; i++)
      for (int j = 0; j < 2; j++)
         for (int k = 0; k < 2; k++)
            MatrixRez[i][j] += Matrix[i][k] * Matrix2[k][j];

   Matrix[0][0] = MatrixRez[0][0];
   Matrix[0][1] = MatrixRez[0][1];
   Matrix[1][0] = MatrixRez[1][0];
   Matrix[1][1] = MatrixRez[1][1];
}

void FastExponentiation(int (&Matrix)[2][2], int exponent)
{
   int temp[2][2] = {
       0, 1,
       1, 1};
   for (int i = 30; i >= 0; i--)
   {
      multiply(Matrix, Matrix);
      if (exponent & (1 << i))
         multiply(Matrix, temp);
   }
}

int main()
{

   FastExponentiation(FibbonaciOperator, 5);

   std::cout << FibbonaciOperator[1][0];

   fflush(stdin);
   std::cin.get();
   return 0;
}

Error is given on "temp" in calling of function "multiply".

But when you replace second argument in function multiply with commented part or to be precise add full dimensions of 2d array error disappears.

My question is why ?

Whole error message :

a reference of type "int (&)[][2]" (not const-qualified) cannot be initialized with a value of type "int [2][2]"

EDIT: Code is a bit different but error is the same , and now error appears on two places where "multiply" is being called , on second argument ofcourse.

luka_bur3k
  • 107
  • 5
  • 2
    You should use `>` only to quote something (e.g. error messages). But not to highlight all text you write. – t.niese Aug 22 '20 at 10:20
  • 1
    Let's see the full code, i.e. how did you call `FastExponentiation`? Where did the first argument in that function come from? – PaulMcKenzie Aug 22 '20 at 10:24
  • I edited the whole code now a bit different since original was a try to simplify things , but error and solution and question stay the same. – luka_bur3k Aug 22 '20 at 10:45
  • 1
    I think that's it: _"References and pointers to arrays of unknown bound can be formed, but cannot be initialized or assigned from arrays and pointers to arrays of known bound."_ https://en.cppreference.com/w/cpp/language/array#Arrays_of_unknown_bound – Thomas Sablik Aug 22 '20 at 11:05
  • I was , you could say , misled by this https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function , yeah it says that it's safest to pass reference with full dimension defined , but if we can pass array with one dimension less and let compiler deduce it's size why can't we do the same with reference ? – luka_bur3k Aug 22 '20 at 11:06
  • The size isn't deduced. You have to pass the size as additional parameter. – Thomas Sablik Aug 22 '20 at 11:07

1 Answers1

1

You are not allowed to initialize a reference to an array with unknown bound with an array of known bound. The second parameter of

void multiply(int (&Matrix)[2][2], int (&Matrix2)[][2])

is a reference to an array of unknown bound and

int temp[2][2]

is an array of known bound. This call

multiply(Matrix, temp);

is not allowed.

"References and pointers to arrays of unknown bound can be formed, but cannot be initialized or assigned from arrays and pointers to arrays of known bound."

https://en.cppreference.com/w/cpp/language/array#Arrays_of_unknown_bound

Thomas Sablik
  • 15,040
  • 7
  • 26
  • 51