0

how may i init below structure with these values:

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

i tried this but 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};

1 Answers1

3

In modern C++11 or later (as your question was originally tagged C++ only) you have what is called aggregate initialization. It works like 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 is not really necessary, but I prefer it for the sake of clarity.

PS: you should get your hands on a good introductory C++ book so you learn the basics of the language.

EDIT

In C (as you re-tagged your question) and pre C++11, you need an equal sign. Furthermore, in C the inner braces are 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()
{}
Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • do u have any idea that CodevisionAVR supports aggregate initialization? ';' expected, but '{' found – hossein valizadeh Jul 04 '16 at 17:03
  • @hosseinvalizadeh I have absolutely no idea. This is a standard feature though, and it should be accepted by any standard C++ compliant compiler. Does your question refer to C? If yes, please re-tag it. – vsoftco Jul 04 '16 at 17:04
  • tag edited ! i get this error: ';' expected, but '{' found – hossein valizadeh Jul 04 '16 at 17:08
  • @hosseinvalizadeh See the updated edit, you need an `=` sign in C, and cannot miss the inner braces. – vsoftco Jul 04 '16 at 17:09
  • @hosseinvalizadeh You're welcome. If the answer addresses your issue, please accept it after a while (or other answer, if any), so other readers know that the issue has been solved. – vsoftco Jul 04 '16 at 19:16