how to define struct array in c

Thread Starter

ep.hobbyiest

Joined Aug 26, 2014
201
Here is my structure and array combination.
Code:
typedef struct
{
  uint16_t states[4];
}abc;

typedef struct
{
  abc dir[2];
}table_t;

table_t array[6] ={};
I tried to defined by many ways and got some errors regarding braces.
Here is one of error i got.

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

How to define this structure of array?
 

WBahn

Joined Mar 31, 2012
30,058
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.
 

WBahn

Joined Mar 31, 2012
30,058
Yes. I was trying to assign the values.

But while assigning i got the errors. and that errors are posted in #1.
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
 

odm4286

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

C:
typedef struct my_structure
{
    some_field1;
    some_field2;
    some_field3;
};

my_structure structure_array[50];

structure_array[2]->some_field2 = some_data;
 

WBahn

Joined Mar 31, 2012
30,058
I haven't played around with structs in a while so bear with me. Try this.

C:
typedef struct my_structure
{
    some_field1;
    some_field2;
    some_field3;
};

my_structure structure_array[50];

structure_array[2]->some_field2 = some_data;
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.
 

MrSoftware

Joined Oct 29, 2013
2,197
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:
typedef struct
{
  uint16_t states[4];
}abc;

typedef struct
{
  abc dir[2];
}table_t;

table_t aTables[2] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
 

MrSoftware

Joined Oct 29, 2013
2,197
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:

MrAl

Joined Jun 17, 2014
11,474
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.
 
Top