gpt4 book ai didi

我可以在不使用 c 中的循环的情况下将 1d 数组复制到 2d 结构中吗?

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:42 24 4
gpt4 key购买 nike

如果我有一个像这样声明的大一维数组

1darray = malloc(N*sizeof(double))

我有另一个这样声明的结构

for j:A

2dstructure[j] = malloc(C[j]*sizeof(double*))

for i:B

2dstructure[j][i] = malloc(D*sizeof(double))

end
end

A*B = N 的大小,即 sizeof(1darray) == sizeof(2dstructure)

有没有一种方法可以在不使用循环的情况下将一维数组中的内容复制到二维结构?

最佳答案

是的,您可以将 2D 索引映射到线性 1D 索引,因此:

int idx = j*B + i;

此时,1darray[idx] 等价于 2dstructure[j][i]

关于我可以在不使用 c 中的循环的情况下将 1d 数组复制到 2d 结构中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16821038/

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