I wrote the following code, however compilation fails with the message
Program received signal SIGSEGV, Segmentation fault. main () at qary_code.cpp:130 out<<four_ary_matrix[i][j]<<" "; (gdb)
I want to pass the 2D array to a function, but the length len will change, so I'm also wondering how to pass the 2D array to the function
code:
int** global_fourary_matrix;
void print_ary(int** ary, int len){
for(int i=0;i<len;i++) {
for(int j=0;j<len;j++) {
cout<<ary[i][j]<<" ";
}
cout<<endl;
}
}
void main(){
int four_ary[][4] ={ {0,0,0,0}, {0,1,2,3}, {0,2,3,1}, {0,3,1,2} };
global_fourary_matrix = (int**) four_ary;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
cout<<global_fourary_matrix[i][j]<<" ";
}
cout<<endl;
}
int len=4;
print_ary(global_fourary_matrix, len);
}
another written style was
global_fourary_matrix = (int**) &(four_ary[0]);
however, the compiler still say the same thing
