c++ - Could not convert from <brace-enclosed initializer list> to int(*) array -
i new c++, , error keep getting.
#include <iostream> #include <string> using namespace std; void print (int test[2][2]= {{1,2},{3,4}}) { cout << test[0][0] << endl; cout << test[1][0] << endl; } int main() { print(); return 0; }
the error is: not convert '{{1, 2}, {3, 4}}' '' 'int (*)[2]'|
i beginner in c++ , still learning.
function parameters declared arrays adjusted implicitly pointers first elements.
so function declaration looks like
void print ( int ( *test )[2] = { { 1, 2 }, { 3, 4 } } );
and pointer may not initialized such way because scalar object.
in fact these function declarations
void print( int test[10][2] ); void print( int test[2][2] ); void print( int test[][2] ); void print( int ( *test )[2] );
are equivalent , declare same 1 function.
however define parameter reference array. in case expected result. example
#include <iostream> void print ( const int ( &test )[2][2] = { { 1, 2 }, { 3, 4 } } ) { std::cout << test[0][0] << std::endl; std::cout << test[1][0] << std::endl; } int main() { print(); int a[2][2] = { { 5, 6 }, { 7, 8 } }; print( ); }
the program output is
1 3 5 7
Comments
Post a Comment