Thread: How to manually initialize an array of structs?

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

    How to manually initialize an array of structs?

    I have the following struct:

    Code:
    struct Frame {
        unsigned char digit;    // Digit:  0 = 1 minutes, 1 = 10 minutes, etc.
        unsigned char seg;      // Segment to turn on:  a - g
        unsigned char dur;      // Duration in msec. to keep it on
    };
    I'd like to create an array of these structs with 50 elements, each of which is initialized to a specific value (I can't do it through a loop). How do I do that? E.g., a 5 element array of ints could be initialized as follows:

    Code:
    int loop1[] = {0, 1, 2, 3, 4};
    I think the array I need would start like:

    Code:
    struct Frame loop1[] = { ??? };
    I don't know how to specify the individual Frame instances with their values within the array. Thanks for your help.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think in C99 you can do:
    Code:
    struct Frame loop[] = { { .digit = ?, .seg = ?, .dur = ? }, /* ... */ };
    The named parameters are just niceties though, you can do it without them - just remember the order.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The syntax would be something like this:
    Code:
    struct Frame loop1[] = {{0, 1, 2},
                            {1, 2, 3},
                            {2, 3, 4},
                            {3, 4, 5},
                            {4, 5, 6}};
    EDIT:
    Quote Originally Posted by Involute
    I'd like to create an array of these structs with 50 elements, each of which is initialized to a specific value (I can't do it through a loop).
    Actually, while you cannot initialise with a loop, you don't necessarily have to initialise in that sense, e.g., instead of hardcoding all 50 elements' initial values, perhaps you could read from file and then use a loop to assign the initial values according to what was read.
    Last edited by laserlight; 05-07-2017 at 06:20 PM.
    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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Thanks, guys. I'll give those a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2015, 11:19 AM
  2. How to initialize an array
    By Tima92 in forum C++ Programming
    Replies: 10
    Last Post: 08-06-2012, 02:38 PM
  3. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. initialize 2D array!
    By samirself in forum C Programming
    Replies: 1
    Last Post: 03-10-2005, 11:56 AM

Tags for this Thread