Complex static memory structures and content

A

Andreas Lundgren

Hi!

I want to put some values in static memory, and then be able to
reference this data from my code in a decent way by referencing
AllData.Data. (All code beneth is placed outside functions.)

___TYPE___
typedef struct Data_s
{
char* Name_p;
void* Class_p;
int Size
} Data_t;

___DATA___
{"foo", NULL, 0},
{"bar", NULL, 0},



I first tried the following, but when compiling for ARM compiler I've
got "incomplete type is not allowed"

typedef struct AllData_s
{
Data_t Data[];
} AllData_t;

static AllData_t AllData = {
{"foo", NULL, 0},
{"bar", NULL, 0}
};



I then tried the following, but got the same error.

typedef struct Data_t AllData_t[2];

static AllData_t AllData = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};



Finally this worked:
typedef struct Data_t AllData_t[2];

static Data_t AllData[2] = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};


Is is possible to build up more advanced static structures? When I've
got this to work, I directly use the AllData and not the
AllData.Data that I really desired. Would it be possible to do a
more complex data structure at compile time?

Best Regards,
Andreas L - Sweden
 
B

Ben Bacarisse

Andreas Lundgren said:
I want to put some values in static memory, and then be able to
reference this data from my code in a decent way by referencing
AllData.Data. (All code beneth is placed outside functions.)

typedef struct Data_s
{
char* Name_p;
void* Class_p;
int Size


(Missing ;)
} Data_t;
Finally this worked:
typedef struct Data_t AllData_t[2];

static Data_t AllData[2] = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};

The typedef of AllData_t is not involved (that is why it works!).
This is the correct way to proceed, but you don't need the 2:

static Data_t AllData[] = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};

is fine.
Is is possible to build up more advanced static structures?

Yes. Just keep building it up from the simple piece to the more
complex ones. If you can use C99 you get even more options, but I
have not shown anything here that is not "old" C.
When I've
got this to work, I directly use the AllData and not the
AllData.Data that I really desired. Would it be possible to do a
more complex data structure at compile time?


If you now want that array of two Data_t objects to be part of bigger
struct, then use a pointer:

struct AllData_s {
Data_t *Data;
size_t n_data_objects;
/* other members ... */
} all_data = {
AllData,
sizeof AllData / sizeof AllData[0]
};

Now you can use all_data.Data[1] if that is what you need (for some
reason).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top