将一个2D数组分配给另一个

时间:2021-12-11 21:30:10

So in my program I have a function which passes into it a 2D array and I want to set another 2D array equal to that 2D array. I am coding this in C++ and can't quite get the syntax right. Right now I have the following:

在我的程序中,我有一个函数它传递一个二维数组我想设置另一个二维数组等于那个二维数组。我用c++编写了这个代码,但语法不太正确。现在我有以下几点:

void MyFunction(float **two_d_array){
    float newArray[4][4];
    //Set new array equal to two_d_array
}

two_d_array will also always be 4x4 so the dimensions themselves aren't an issue.

two_d_array将始终是4x4,因此维度本身不是问题。

3 个解决方案

#1


5  

I hope you are not passing a two-dimensional array as a double pointer to your function.

我希望您不会将二维数组作为双指针传递给函数。

Anyways, you can just write

不管怎样,你可以写下来

for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        newArray[i][j] = two_d_array[i][j];

If you have another two-dimensional array (and not an array of pointers), then simply use memcpy():

如果有另一个二维数组(而不是指针数组),那么只需使用memcpy():

void foo(float arr[][4])
{
    float newArray[4][4];
    memcpy(newArray, arr, sizeof(newArray));
}

#2


1  

When you define a two dimentional array as

当你定义一个二维数组为

float a[4][4]

Its type is float [4][4].

它的类型是浮动[4][4]。

if you want to pass float** to the function you can create your with float**

如果要将float** *传递给函数,可以使用float**创建

float** f = (float**) malloc(sizeof(float *)*4);
    for(int i=0;i<4;i++){
      f[i] = (float*) malloc(sizeof(float)*4);
    }
//initialize
MyFunction(f);

And Myfunction will be similar

我的函数是相似的

void MyFunction(float **two_d_array){
    float newArray[4][4];
    for(int i=0;i<4;i++){
        float* one_d = & two_d_array[i][0];
        for(int j=0;j<4;j++){
            newArray[i][j] = one_d[j];
        }   
    }

}

#3


0  

Yes, you must assign the second dimenson, and the first dimension passing by a integer parameter, it will look like this: void foo(float arr[][4], int dim);

是的,你必须指定第二个维度,第一个维度通过一个整数参数传递,它看起来是这样的:void foo(float arr[][4], int dim);

#1


5  

I hope you are not passing a two-dimensional array as a double pointer to your function.

我希望您不会将二维数组作为双指针传递给函数。

Anyways, you can just write

不管怎样,你可以写下来

for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        newArray[i][j] = two_d_array[i][j];

If you have another two-dimensional array (and not an array of pointers), then simply use memcpy():

如果有另一个二维数组(而不是指针数组),那么只需使用memcpy():

void foo(float arr[][4])
{
    float newArray[4][4];
    memcpy(newArray, arr, sizeof(newArray));
}

#2


1  

When you define a two dimentional array as

当你定义一个二维数组为

float a[4][4]

Its type is float [4][4].

它的类型是浮动[4][4]。

if you want to pass float** to the function you can create your with float**

如果要将float** *传递给函数,可以使用float**创建

float** f = (float**) malloc(sizeof(float *)*4);
    for(int i=0;i<4;i++){
      f[i] = (float*) malloc(sizeof(float)*4);
    }
//initialize
MyFunction(f);

And Myfunction will be similar

我的函数是相似的

void MyFunction(float **two_d_array){
    float newArray[4][4];
    for(int i=0;i<4;i++){
        float* one_d = & two_d_array[i][0];
        for(int j=0;j<4;j++){
            newArray[i][j] = one_d[j];
        }   
    }

}

#3


0  

Yes, you must assign the second dimenson, and the first dimension passing by a integer parameter, it will look like this: void foo(float arr[][4], int dim);

是的,你必须指定第二个维度,第一个维度通过一个整数参数传递,它看起来是这样的:void foo(float arr[][4], int dim);