-1

I want to pass two 2D arrays into a function in order to copy the whole array. I am using double pointers to do this but it is showing error.

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

It is showing error for the 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)]' to 'float**' for argument '1' to 'void copy_arr(float**, float**, int, int)'

Casey
  • 10,297
  • 11
  • 59
  • 88
kumar649
  • 1
  • 1

1 Answers1

0

In C doesn't natively exist the concept of multidimensional array. I.e. what we call a bidimensional array:

float fArray[10][10];

Is in reality interpreted by compiler as an array of 10 arrays of float. For this reason the operator [], that can only retrieve values from single arrays, needs to know all following dimensions to access a single element.
Said that you have two ways to pass a multidimensional array, the first case apply when the second dimension is known (extending the concept to multidimensional arrays all other dimensions, but the first, must be know):

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

If the dimensions are variable you must compute yourself the pointer to the 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 is the last element: %f\n", table + ((m-1)*n + (n-1))*sizeof(float));
    return ;
}

In C++ you can use std::vector

Frankie_C
  • 4,764
  • 1
  • 13
  • 30