gpt4 book ai didi

将 2D 数组转换为 3D?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:50 25 4
gpt4 key购买 nike

这是取一半 2D 数组并将其“转换”为 3D 数组的正确方法吗?

#define CH_2D (6)
#define CH_3D (3)

float w_2d [65*CH_2D][2];
float (*w_3d)[65][CH_3D][2] = (void *) w_2d;

这会访问原始 w_2d 的正确元素吗:

for(int i=0; i<65; i++) 
{
for(int j=0; j<CH_3D; j++)
for(int k=0; k<2; k++)
float temp = (*w_3d)[i][j][k];
}

最佳答案

我相信您的尝试有 2 个问题。

Is this a proper way of taking half of 2D array and "converting" it to a 3D array?

不是。当实际类型为 float[390][2] 时,您正在取消引用类型为 float[65][3][2] 的指针,这是严格的别名违规。要解决这个问题,您需要通过禁用严格别名将您的 C 编译器置于非标准模式,或者只是重写您的代码以不这样做。

Will this access the right elements of original w_2d:

这取决于您如何将数据存储在原始数组中。如果数据的存储顺序类似于:

float[2][65][3][2]

但是,从您的 w_2d 声明怀疑您的数据排列如下:

float[65][6][2] 
or
float[6][65][2]

在这种情况下,我怀疑你会得到你想要的元素。

关于将 2D 数组转换为 3D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55589300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com