how to pass 2 dimensional array if both dimensions are unknown at compile time

后端 未结 5 1618
[愿得一人]
[愿得一人] 2021-01-20 16:26

what is this the correct way to pass 2 dimensional array of unknown size?

reprVectorsTree::reprVectorsTree(float tree[][], int noOfVectors, int dimensions)
<         


        
相关标签:
5条回答
  • 2021-01-20 16:28
    #include <iostream>
    using namespace std;
    
    int
    main(void)
    {
        int layers = 3; // can be calculated in run-time
        int elem = 5; // can be calculated in run-time
        int* pArray = new int[layers * elem];
        /* usage */
        for (int i = 0; i < sizeof(pArray) / sizeof(pArray[0]); ++i) // traverse through layers
        {
    
            for (int j = 0; j < sizeof(pArray[0])/ sizeof(int); ++j) // traverse through elements in layer
            {
                // do some stuff
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-20 16:37

    In modern C, starting from C99, it is a simple as that

    void foo(size_t n, size_t m, double A[n][m]) {
      //
    }
    

    and here you go. The only thing that you'd have to have in mind is that the sizes must come before the array in the argument list.

    To avoid to allocate such a beast on the stack on the calling side you should just do

    double (*A)[m] = malloc(sizeof(double[n][m]));
    

    such a "matrix" can be used as you are used to with something like A[i][j] and a call to foo would just look like

    foo(n, m, A);
    
    0 讨论(0)
  • 2021-01-20 16:39

    First idea was to use vectors. But if you are working with a C code, pass it as a reprVectorsTree(float** tree, int noOfVectors, int dimensions).

    For your case:

    float tree[15][2] = {{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1}};
    
    int nRows = 15;
    int nCols = 2;
    
    float** arr = new float*[nRows];
    
    for (int i = 0; i < nRows; ++i) {
         arr[i] = new float[nCols];
    }
    
    for (int i = 0; i < nRows; ++i) {
        for (int j = 0; j < nCols; ++j) {
            arr[i][j] = tree[i][j];
        }
    }
    
    reprVectorsTree *r1 = new reprVectorsTree(arr, nRows, nCols);
    
    0 讨论(0)
  • 2021-01-20 16:41

    Use pointers..

         reprVectorsTree(tree, noOfVectors, dimensions);// Calling function.
    

    Function Definition:

    reprVectorsTree(float **tree, int noOfVectors, int dimensions){
    
    
    }
    

    I think it will be helpful to you.

    0 讨论(0)
  • 2021-01-20 16:43

    If the size is unknown, you can use a simple float *tree pointer to a 1D array. The syntax for turning to particular elements wouldn't be as of 2D arrays however:

    reprVectorsTree::reprVectorsTree(float *tree, int noOfVectors, int dimensions)
    {
        ...
        tree[ row_number * dimensions + column_number ] = 100.234;
    }
    

    In the calling code you will have something like this:

    float d2array[ROWS][COLUMNS];
    ...
    reprVectorsTree(&d2array[0][0], ROWS, COLUMNS);
    

    updated

    Consider the following example of different approaches of passing a 2D array:

    #include <iostream>
    #include <malloc.h>
    
    float test[2][4] = 
    {
       {3.0, 4.0, 5.0, 0},
       {6.0, 7.0, 8.0, 0}
    };
    
    void print(float *root, int rows, int columns)
    {
       for (int row = 0; row < rows; ++row)
       {
          for (int col = 0; col < columns; ++col)
          {
             std::cout << root[row * columns + col ] << " ";
          }
          std::cout << std::endl;
       }
    }
    
    float *test2[2] = 
    {
       &test[0][0],
       &test[1][0],
    };
    
    void print2(float **root, int rows, int columns)
    {
       for (int row = 0; row < rows; ++row)
       {
          for (int col = 0; col < columns; ++col)
          {
             std::cout << root[row][col] << " ";
          }
          std::cout << std::endl;
       }
    }
    
    int main()
    {
       print(&test[0][0], 2, 4);
       //print(test2, 2, 4); // doesn't work
    
       print2(test2, 2, 4);
       //print2(&test[0][0], 2, 4); // doesn't work
       //print2(&test[0], 2, 4); // doesn't work
    
       float **dynamic_array = (float **)malloc(2 * sizeof(float *));
       dynamic_array[0] = (float *)malloc(4 * sizeof(float));
       dynamic_array[1] = (float *)malloc(4 * sizeof(float));
    
       for (int row = 0; row < 2; ++row)
       {
          for (int col = 0; col < 4; ++col)
          {
             dynamic_array[row][col] = (float)(row * 4 + col);
          }
       }
    
       print2(dynamic_array, 2, 4);
       //print(dynamic_array, 2, 4); // doesn't work
    
       return 0;
    }
    
    0 讨论(0)
提交回复
热议问题