c - struct array element initialization -


how may init below structure these values:

struct test_str { unsigned char add[6]; unsigned int  d; unsigned char c; }my_str; 

i tried resulted in error:

struct test_str { unsigned char add[6]; unsigned int  d; unsigned char c; }my_str {.add[0]=0x11,.add[0]=0x22,.add[0]=0x33,          .add[0]=0x44,.add[0]=0x55,.add[0]=0x66,          .d=0xffe,.c=10}; 

in modern c++11 or later (as question tagged c++ only) have called aggregate initialization. works this:

struct test_str {     unsigned char add[6];     unsigned int  d;     unsigned char c; } my_str { {0x11,  0x22, 0x33, 0x44, 0x55, 0x66},             0xffe,              10          };  int main() {} 

live on coliru

the inner pair of braces not necessary, prefer sake of clarity.

ps: should hands on introductory c++ book learn basics of language.

edit

in c (as re-tagged question) , pre c++11, need equal sign. furthermore, in c inner braces not optional:

struct test_str {     unsigned char add[6];     unsigned int  d;     unsigned char c; } my_str = { {0x11,  0x22, 0x33, 0x44, 0x55, 0x66},              0xffe,               10            };  int main() {} 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -