如果两个维在编译时都是未知的,则如何传递2维数组 [英] how to pass 2 dimensional array if both dimensions are unknown at compile time

查看:125
本文介绍了如果两个维在编译时都是未知的,则如何传递2维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是正确的方式传递未知大小的二维数组?

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

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

如何访问此数组的元素函数?

how to access the elements of this array later in the function?

如何从调用函数传递一个二维数组?

How to pass a 2 dimensional array from the calling function?

----- edit ----

-----edit----

我想使用数组,因为调用是从ac代码完成的,并且有ac到c ++ interface

I want to do with an array as the calling is done from a c code and there is a c to c++ interface

----- edit -----
如何定义从调用函数传递二维数组?

-----edit----- How to define pass a 2 dimensional array from the calling function?

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}};

reprVectorsTree *r1 = new reprVectorsTree(tree[0][0],8,2);

上述代码有什么问题?
我得到一个不能将参数1从'float'转换为'float **'

what is wrong with the above code? I get a cannot convert parameter 1 from 'float' to 'float **'

推荐答案

未知,可以使用一个简单的 float * tree 指针指向一个1D数组。转向特定元素的语法不会像2D数组那样:

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);

已更新

考虑以下传递二维数组的不同方法的例子:

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;
}

这篇关于如果两个维在编译时都是未知的,则如何传递2维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆