how to define struct array in c

Discussion in 'Programmer's Corner' started by ep.hobbyiest, Aug 8, 2016.

  1. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    Here is my structure and array combination.
    Code (Text):
    1.  
    2. typedef struct
    3. {
    4.   uint16_t states[4];
    5. }abc;
    6.  
    7. typedef struct
    8. {
    9.   abc dir[2];
    10. }table_t;
    11.  
    12. table_t array[6] ={};
    13.  
    I tried to defined by many ways and got some errors regarding braces.
    Here is one of error i got.

    Code (Text):
    1.  
    2. error: extra brace group at end of initializer
    3.             {1,2,3,4}
    4.             ^
    5. error: (near initialization for 'ct[0].dir[0]')
    6. warning: excess elements in struct initializer
    7. warning: (near initialization for 'ct[0].dir[0]')
    8. error: extra brace group at end of initializer
    9.                            {1,2,3,4}
    10.  

    How to define this structure of array?
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    356
    Type define a truct and then define an array of the typedefined type.
     
  3. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    I think the same thing i did.
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    The problem is that you are trying to initialize an array of structs to something that doesn't make sense for an array of structs to be initialized to.
     
  5. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    Yes. I was trying to assign the values.

    But while assigning i got the errors. and that errors are posted in #1.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    That's because you aren't doing it correctly. When you provide an initializer for a struct, it can't be an empty set of braces.

    Look at the material here for some guidance:

    http://en.cppreference.com/w/c/language/struct_initialization

    BTW -- that site was found using Google with the following search terms: initialize struct in C
     
  7. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    I haven't played around with structs in a while so bear with me. Try this.

    Code (C):
    1.  
    2. typedef struct my_structure
    3. {
    4.     some_field1;
    5.     some_field2;
    6.     some_field3;
    7. };
    8.  
    9. my_structure structure_array[50];
    10.  
    11. structure_array[2]->some_field2 = some_data;
    12.  
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    What he's trying to do is to initialize all of the structures in an array of structs (and in which each element is a struct that contains a struct within it) at the same time that he declares the array itself.

    Should be possible and I think the link I provided gives enough info to do it. Personally, I seldom initialize variables at declaration time as I prefer to specifically initialize just those variables that have a specific need to be initialized to specific values. So I would probably write a function to initialize each type of struct. But that's a personal preference and the propriety of this approach can be argued both pro and con.
     
  9. MrSoftware

    Member

    Oct 29, 2013
    497
    122
    First, if you only want to declare the array and not initialize it, then just declare it:

    table_t aTables[2];


    With nested structs like this, there are easier to read ways to initialize it than when you declare it, but if you must (this is really ugly, don't ever do this in real code) the following compiles and initializes all your elements. Use the variable and memory watch tools in your debugger to see where these values end up.

    Code (Text):
    1.  
    2. typedef struct
    3. {
    4.   uint16_t states[4];
    5. }abc;
    6.  
    7. typedef struct
    8. {
    9.   abc dir[2];
    10. }table_t;
    11.  
    12. table_t aTables[2] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
    13.  
     
  10. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    Thanks MrSoftware,

    This idea is really good. no missing brace problem.
     
  11. MrSoftware

    Member

    Oct 29, 2013
    497
    122
    Technically it works, but it's not a good idea. It makes for delicate code. If one of the structs changes, your initialization will be broken, and it might not be obvious. Say you're initializing dir[1].state[2] to a specific value using the code above. Some time from now, maybe a co-worker or maybe yourself comes along and decides to add an extra element inside struct abc, maybe they add another int:

    typedef struct
    {
    uint32_t more_states[2];
    uint16_t states[4];
    }abc;

    Now your initialization is broken, the wrong values will go in the wrong fields and you might not notice until it causes problems. IMHO, it's much better to initialize the fields explicitly. It's easier to read and makes for more durable code.

    While we're on the topic; it's also possible to use memcpy, fread, etc.. to directly initialize fields, but this too can be problematic. Depending on the platform, your compiler settings, etc.. things like padding can change and you can end up with a bad result. IMHO it's almost always better to access fields directly by name, unless you have a very specific reason to access by address instead.
     
    Last edited: Aug 11, 2016
  12. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    356
    I think you will find it generally a good practice to initialize your variables not in the initialization process for the main but in a dedicated routine, and use brackets correctly.
     
  13. MrAl

    Well-Known Member

    Jun 17, 2014
    2,418
    488
    Hi,

    I dont use structs that much anymore either but this looks like a problem that would better be solved with a multidimensional array. I dont really know how the data has to be accessed though in the actual application after all the initialization is over with.
     
Loading...