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

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    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. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Have a look at Arrays in C - Cprogramming.com for a start (multidimensional arrays are discussed about halfway down the page).

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    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;
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-17-2011, 08:53 PM
  2. How to store bulk data and/or point to array?
    By GuyIncognito in forum C Programming
    Replies: 1
    Last Post: 05-20-2011, 11:34 PM
  3. Replies: 1
    Last Post: 04-14-2011, 11:15 AM
  4. Pick data from file store in array
    By swgh in forum C Programming
    Replies: 1
    Last Post: 07-10-2009, 09:57 AM
  5. Data Structure to store unlimited dynamic vectors
    By m3rk in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2009, 06:12 AM