-3

What I am trying to do is initialize the struct with a large about of hex data stored in a separate part of the device's memory, eventually this data will be written to memory through other means but for now I want to manually initialize the struct to use in my project.

The project I am working on has the following lines of code (that compile in Atmel studio).

typedef struct configData_t
{
    uint8_t version[4]; // ASCII

    uint8_t numIn;
    uint8_t numOut;
    uint8_t numKey;
    uint8_t numTest;
    uint8_t numAuto;

    controlModuleConfig_t homeConfig;

    inputModuleConfig_t  inConfig  [MAX_IN];
    outputModuleConfig_t outConfig [MAX_OUTPUT];
    keypadModuleConfig_t keyConfig [MAX_KEY];
    notificationConfig_t testConfig [MAX_TEST];
    autoFunctionConfig_t autoConfig [MAX_AUTO];

    precheckConfig_t precheckConfig;
    sleepConfig_t    sleepConfig;

    uint16_t audioCrc16;
    uint16_t configCrc16;
}
configData_t;

const __attribute__((__section__(".application_footer_data"))) 
configData_t theConfigData = { { '?', '?', '?', '?' } };

__attribute__ ((section(".application_footer_data"))) 
const unsigned char configBuffer[28672] = { /* Lots of data e.g. 0x31, 0x30, 0x33,...*/}

I have not seen a struct initialized this way before, how is theConfigData struct being initialized with the { { '?', '?', '?', '?' } } statement?

Aaron Thompson
  • 1,849
  • 1
  • 23
  • 31

2 Answers2

1

The line

configData_t theConfigData = { { '?', '?', '?', '?' } };

will initialize each element of version data member to ASCII value of '?'. Rest of the member of the struct will be initialized to '0'.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Will the configBuffer[28672] set the rest of the struct data? – Aaron Thompson Apr 12 '18 at 15:29
  • 1
    Do you see the problem of asking 2 questions in 1 post? People read your title and the 99% of the post that focus on #1 and don't notice that you throw #2 in there at the end. I would recommend moving that to another, new post. – underscore_d Apr 12 '18 at 15:43
0

The outer set of braces denotes the initializer for the struct. Since the first element of the struct is an array of uint8_t, the second set of braces initializes the elements of that array.

The remaining elements, which have no explicit initializer, are implicitly initialized to all 0 values.

dbush
  • 205,898
  • 23
  • 218
  • 273