Thread: Array of arrays of structs

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    10

    Array of arrays of structs

    I have the following struct:

    Code:
    struct Frame {
        unsigned char digit;    
        unsigned char seg;     
        unsigned char onoff;   
        unsigned char dur;      
    };
    I have several arrays of these structs, all different lengths. I'd like to pull these arrays together into a single array so I can access them more easily. Something that would let me do something like this (in pseudo code):

    Code:
    minor_array1[] = {Frame, Frame, Frame};
    minor_array2[] = {Frame, Frame, Frame, Frame, Frame};
    minor_array3[] = {Frame, Frame, Frame, Frame};
    
    master_array[] = {minor_array1, minor_array2, minor_array3};
    
    for (i = 0; i < 3; i++) 
      for (j = 0; j < length_of(master_array[i]); j++) {
        master_array[i][j].digit = 6;
        master_array[i][j].seg = 7;
        master_array[i][j].onoff = 8;
        master_array[i][j].dur = 9;
      }
    Thanks for any suggestions.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you want an array of arrays of Frame objects. You would need to decide on what is the maximum number of Frame objects there could be in an array, then create an array of arrays of that size, with some way to denote an unused element (e.g., a Frame object with all members zero).

    Alternatively, it could be that an array of Frame objects has some domain-specific meaning, then perhaps you could define a struct type having an array of Frame objects (again you would need to know the maximum number of Frame objects, unless you use a dynamic array) along with whatever other members, then create an array of objects of this struct type. This could also be useful for recording how many elements are in use, e.g., by having an integer member to record this number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Thanks for the tips. I can do either one, but I can't figure out how to define and initialize master_array. If I try the obvious way listed above, my compiler throws an error (something like type mismatch, but I'm not at my PC right now).

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    To me at least it seems like you should just accept that there will be some unused Frames in the array. Decide on some values which mean "unused frame" and place accordingly.

    struct Frame master[3][5];

    This is basically what you asked for, three arrays of five Frames.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Thanks, but how do I make the assignments? Do I have to assign each element of each minor array to a corresponding element in the master array, or is there a way to assign each entire minor array to the master?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot assign to arrays, even when they are elements of an array of arrays, so assigning member-wise is one viable approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Can you show an example?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You know how. You've written similar code, just try.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    I didn't understand laserlight's reply, which is why I asked for an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel arrays vs array of structs
    By Brian Swisher in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2012, 06:39 AM
  2. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  3. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM

Tags for this Thread