Reputation: 16195
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);
Upvotes: 3
Views: 300
Reputation: 426
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++];
}
}
}
Upvotes: 1