Like Tree1Likes
  • 1 Post By Malcolm McLean

How to store data structure into 2D array?

This is a discussion on How to store data structure into 2D array? within the C Programming forums, part of the General Programming Boards category; Hi I have this data store into a data structure. i j x y w h w*h 0 0 0 ...

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

    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
    333
    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;
    stahta01 likes this.
    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.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21