[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] How to initialize an array of struct?
From: |
Russell Shaw |
Subject: |
Re: [avr-gcc-list] How to initialize an array of struct? |
Date: |
Sun, 15 Dec 2013 18:03:54 +1100 |
User-agent: |
Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20131005 Icedove/17.0.9 |
On 15/12/13 17:20, dfx wrote:
Hi,
After spending more than one day to create an array of struct I have
come to this code:
struct Lamp {
uint8_t highAddress [9] ;
uint8_t lowAddress [9] ;
uint8_t onoff ;
uint8_t active ;
uint8_t power;
uint8_t brightness ;
uint8_t temperatures ;
uint8_t faultCount ;
} ;
struct Lamp lamps [10];
void init () {
struct Lamp lamp0 = {" 0013A200 ", " 4094500D " , 0 , 1 , 0 , 0, 0 , 0} ;
struct Lamp lamp1 = {" 0013A200 ", " 40B12530 " , 0 , 1 , 0 , 0, 0 , 0} ;
struct Lamp lamp2 = {" 11111111 ", " 22222222 ", 0 , 0, 0 , 0, 0, 0 };
struct Lamp lamp3 = {" 33333333 ", " 44444444 ", 0 , 0, 0 , 0, 0, 0 };
struct Lamp lamp4 = {" 55555555 ", " 66666666 ", 0 , 0, 0 , 0, 0, 0 };
struct Lamp lamp5 = {" 77777777 ", " 88888888 ", 0 , 0, 0, 0 , 0, 0 };
struct Lamp lamp6 = {" 99999999 ", " 00000000 ", 0 , 0, 0, 0 , 0, 0 };
struct Lamp lamp7 = {" AAAAAAAA ", " BBBBBBBB ", 0 , 0, 0, 0 , 0, 0 };
struct Lamp lamp8 = {" CCCCCCCC ," " DDDDDDDD ", 0 , 0, 0, 0 , 0, 0 };
struct Lamp lamp9 = {" EEEEEEEE ", " FFFFFFFF ", 0 , 0, 0, 0 , 0, 0 };
lamps [0] = lamp0 ;
lamps [1] = lamp1 ;
lamps [2] = lamp2;
lamps [3] = lamp3 ;
lamps [4] = lamp4 ;
lamps [5] = lamp5 ;
lamps [6] = lamp6 ;
lamps [7] = lamp7 ;
lamps [8] = lamp8 ;
lamps [9] = lamp9 ;
}
it compiles without errors ( and it may be that it works!) but it is
certainly wrong because it consumes twice the memory required (once for
the array [ 10] and another for 10 lamps).
The question is: how can I initialize the array without defining first
ten lamps individually ?
Thank you very much .
struct Lamp {
uint8_t *highAddress;
uint8_t *lowAddress;
uint8_t onoff;
uint8_t active;
uint8_t power;
uint8_t brightness;
uint8_t temperatures;
uint8_t faultCount;
} ;
struct Lamp lamps [10] = {
{(uint8_t *)" 0013A200 ", (uint8_t *)" 4094500D ", 0, 1, 0, 0, 0, 0},
{(uint8_t *)" 0013A200 ", (uint8_t *)" 40B12530 ", 0, 1, 0, 0, 0, 0},
{(uint8_t *)" 11111111 ", (uint8_t *)" 22222222 ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" 33333333 ", (uint8_t *)" 44444444 ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" 55555555 ", (uint8_t *)" 66666666 ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" 77777777 ", (uint8_t *)" 88888888 ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" 99999999 ", (uint8_t *)" 00000000 ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" AAAAAAAA ", (uint8_t *)" BBBBBBBB ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" CCCCCCCC ", (uint8_t *)" DDDDDDDD ", 0, 0, 0, 0, 0, 0},
{(uint8_t *)" EEEEEEEE ", (uint8_t *)" FFFFFFFF ", 0, 0, 0, 0, 0, 0},
};
The casts can be eliminated if you use normal "char *" instead of uint8_t.