# Thread: How to store data structure into 2D array?

1. ## How to store data structure into 2D array?

Hi
I have this data store into a data structure.

i j x y w h w*h
0 0 0 0 9 11 99
1 0 0 11 9 10 90
2 0 0 21 9 11 99
0 1 9 0 8 12 96
1 1 9 12 8 7 56
2 1 9 19 8 6 48
3 1 9 25 8 7 56
0 2 17 0 7 8 56
1 2 17 8 7 8 56
2 2 17 16 7 8 56
3 2 17 24 7 8 56
0 3 24 0 8 6 48
1 3 24 6 8 7 56
2 3 24 13 8 6 48
3 3 24 19 8 7 56
4 3 24 26 8 6 48

Code:
```struct data {
//Here
/*! horizontal position */
int x;
/*! vertical position */
int y;
/*! width */
int w;
/*! height */
int h;
/*! i'th rectangle in column */
int i;
/*! j'th column */
int j;
/*! total area */
int w*h;
} data
and then i have an array
/*! it contain group_id of each data line*/
int group_id[16]={0,0,0,0,3,3,1,1,1,3,3,2,2,2,2,1};```
I never worked with 2D array before. My problem is that i want to create a 2D array of that data for example if i write data[j][i].. It will give me the reference/value of all data lines that belongs to j column, and same with group_id[j][i].
I don't know how i can store these structure vaules in this like 2D array.
Need your help..
Thanks

2. Have a look at Arrays in C - Cprogramming.com for a start (multidimensional arrays are discussed about halfway down the page).

Bye, Andreas

3. Most introductory books on C introduce 2D and higher arrays shortly after explaining 1D arrays, without really pointing out the major problems with them in the C language.
The central problem is that a 1D array and a pointer to a block of memory are, to all intents and purposes, equivalent. Multi-dimensional arrays are more difficult, because the dimensions have to be fixed. Trying to size Nd arrays dynamically causes major problems for C.

So most experienced C programmers use 1D arrays, even in situations where more dimensions would be natural, such as image buffers or voxel-based universes. You then pass in width, height and depth, and calcuate the memory address by z *(width*height) + y*width + x;

Popular pages Recent additions