Dieses Alpaka
Dieses Alpaka

Reputation: 13

Argument of type int[8][8] gets turned into int(*)[8]

using T=int[8][8];    //1
                      //2
T a={0};              //3
void f(T b){          //4
     a=b;             //5 
}                     //6

Even though a and b should both be of type T when the function f is called, my compiler says there's an error in line 5: "incompatible types in assignment of 'int(*)[8]' to 'T' {aka 'int [8][8]'}". Why is b not of type T like I declared in line 4?

Upvotes: 1

Views: 61

Answers (1)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

In C++ (and C) you cannot pass an array by value to a function. If you declare a function with an array as it's argument, the type of the argument is "adjusted" to be a pointer. For that reason, your argument is a pointer. You can pass it by reference:

void f(T const& b);

That would be an array reference, but you can't really use = to assign an array. You could use a loop, memcpy, or std::copy to do that. Or, you can use std::array for friendly value semantics:

using T = std::array<std::array<int, 8>, 8>;
                      
T a; 
void f(T const& b) { // could also pass by value
     a = b; // assignment is OK
}  

Upvotes: 1

Related Questions