c++ - passing 2D arrays into a function -


i want pass 2 2d arrays function in order copy whole array. using double pointers showing error.

void copy_arr(float **new_table,float **table,int m,int n) {     //code     return; } 

it showing error 2d array 'new_table' only.

the function calling :

void optimize(float **table,int m,int n) {     int pivot[2];     find_pivot(table,m,n,pivot);     float new_table[m][n];     //code     copy_arr(new_table,table,m,n);     return; } 

error: cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' 'float**' argument '1' 'void copy_arr(float**, float**, int, int)'

in c doesn't natively exist concept of multidimensional array. i.e. call bidimensional array:

float farray[10][10]; 

is in reality interpreted compiler array of 10 arrays of float. reason operator [], can retrieve values single arrays, needs know following dimensions access single element.
said have 2 ways pass multidimensional array, first case apply when second dimension known (extending concept multidimensional arrays other dimensions, first, must know):

void copy_arr(float new_table[][10], float table[][10], int m) {     //code     return ; } 

if dimensions variable must compute pointer element:

void copy_arr(float *new_table, float *table, int m, int n) {     //code     memcopy (new_table, table, sizeof(float)*m*n);     //access last element assuming table[m][n]     printf("this last element: %f\n", table + ((m-1)*n + (n-1))*sizeof(float));     return ; } 

in c++ can use std::vector


Comments