Creating & initialising a const struct

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating & initialising a const struct

Jump to solution
1,397 Views
FridgeFreezer
Senior Contributor I

I'm trying to do something which I'm sure is easily done but I can't find the right way of doing it.

 

I have a structure type in my code something like this:

 

struct mystruct{    uint8  valid;    uint8  foo;    uint8  bar;    uint16 cats;    uint32 dogs;    uint8 checksum;}

 

 

which is then part of a larger structure:

 

struct everything{  struct otherstruct something;  struct mystruct some_things[NUMBER_OF_THINGS];  ...etc.}

This resides in RAM and the contents are modified by the software.

 

I would like to be able to "reset" the entire struct to defaults by creating a const like:

 

const struct mystruct default ={     valid = TRUE;     foo = 0x64;     bar = 10;     cats = 300;     dogs = 9999999;     checksum = 0xFF;}

 

 

and then doing a simple one-line copy:

everything->some_things[2] = default;

Rather than having to set each individual value.

 

I know I can do this:

const struct mystruct default = {TRUE, 0x64, 10, 300, 9999999, 0xFF}

But it's not as readable and requires things to be in the correct order.

 

I'm sure I'm missing the obvious here, can anyone help?

Labels (1)
0 Kudos
Reply
1 Solution
1,047 Views
FridgeFreezer
Senior Contributor I

I think I've found the answer, via Stackoverflow (http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c):

 

typedef struct Foos{
    int a;
    int b;
}Foos;

And the following definition of a const struct of type Foos

static const Foos foo ={
    .a =1,
    .b =2,
};

View solution in original post

0 Kudos
Reply
3 Replies
1,047 Views
ipa
Contributor III

Hi,

I have done this in several projects:

typedef union{ // for DS1390 chip !!!
uint8_t tm[8];
struct{
uint8_t frsec; // fractions of seconds, BCD 0;
uint8_t seconds; // 00-59,BCD 1;
uint8_t mins; // 00-59,BCD 2;
uint8_t hrs; // 00-23,BCD; bit6 must be 0 3;
uint8_t wday; // week day, not used 4;
uint8_t mday; // month day, 00-31,BCD 5;
uint8_t month; // 0x80 | month; bit7 = century 6;
uint8_t year; // year, 00-99, BCD; offset+2000 7;
}dt;
}dtime;

The advantage is tm[], which can be filled up as simple memory locations, using memcpy(). The only problem can appear if you are interested by portability.

Of coarse this is not the only solution for you.

Regards,

Ipa 

0 Kudos
Reply
1,047 Views
FridgeFreezer
Senior Contributor I

ipa - I don't see how that is any different to having to write:

const defaults = { 1, TRUE, 'a', 0,0,0,57,0x8000,0,0,0} etc.

 

When I want to be able to write something like this:

 

const defaults = {
frsec = 0;
year = 2000;

seconds = 0;
mins = 2;
hrs = 3;
};

With named variables being initialised, without having to type all of them in the exact order, and any which aren't named being left as zero.

 

I already know I can reset the "live" version to this stored (in flash) default by doing this:

settings = defaults;

Which copies the structure data across like any other variable.

0 Kudos
Reply
1,048 Views
FridgeFreezer
Senior Contributor I

I think I've found the answer, via Stackoverflow (http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c):

 

typedef struct Foos{
    int a;
    int b;
}Foos;

And the following definition of a const struct of type Foos

static const Foos foo ={
    .a =1,
    .b =2,
};
0 Kudos
Reply