简体   繁体   中英

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 . . . .

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

The data that I want to squeeze into myArray is of type type* . 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++];
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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