Then you convert x & y to linear dimensions.

#define X_DIM 4

#define Y_DIM 4

int linear[X_DIM * Y_DIM];

So equivalent of matrix[x][y] would be:

linear[x + y * X_DIM]

Solved

Posted on 2011-04-30

Hi, i want to retrieve a 2D array from a linear array data

How do i do it? :)

How do i do it? :)

7 Comments

Then you convert x & y to linear dimensions.

#define X_DIM 4

#define Y_DIM 4

int linear[X_DIM * Y_DIM];

So equivalent of matrix[x][y] would be:

linear[x + y * X_DIM]

const int rows=10;

const int columns=5;

int matrix2D[rows][columns];

int r=0;

int c=0;

for (int i=0;i<linearArray.length;i

{

//get the next element of linear array into the next cell of 2D array

}

Hope this helps

(taking the necessary tests and saftey checks first, of course)

```
typedef struct
{
int row[10];
int col[5];
}intArray;
int array[] = {1,2,3,4.5.6.7.8,9,10,11,..........};
// for a quick copy of the flat array into the 2D array
memcpy( &intArray, array, sizeof( array))
```

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Process does stop by itself | 37 | 122 | |

Macro will not compile after project converted from IAR to Microchip xc8 (in MPLab) | 3 | 150 | |

Adjust Mfcapp | 29 | 141 | |

Passing a array as parameter - C | 2 | 70 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!