简体   繁体   English

如何重塑C样式数组

[英]How to reshape c-style arrays

For example, I have 1 dimensional array of 20 elements that I want to reshape into a 4x5 array so, I can pass it to a function with a signature like the following . 例如,我有20个元素的一维数组,我想将其重塑为4x5数组,因此,我可以将其传递给具有如下签名的函数。 . . .

void func(type** inArray, int xDimension, int yDimension)

The data that I want to squeeze into myArray is of type type* . 我要压缩到myArraytype* What do i need to do in order to reshape the array? 我需要做什么才能重塑阵列? I do not want to allocate a new array and use loops to fill the new array. 我不想分配一个新数组并使用循环来填充新数组。 Is there any way to cast the data in place? 有什么方法可以将数据转换到位吗?

The closest I have got is something like this . 我最近的是这样的。 .

float one[20];
float* two[] = {one, one+5, one+10, one+15};
func( two, 4, 5);
void func(type** myArray, int xDimension, int yDimension, type* oneDArray)
{
    int i,j,index=0;
    for(i=0;i<yDimension;i++)
    {
         for(j=0;j<xDimension;j++)
         {
             myArray[i][j]= oneDArray[index++];
         }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM