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 Answer 1

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()
{}
Sign up to request clarification or add additional context in comments.

5 Comments

do u have any idea that CodevisionAVR supports aggregate initialization? ';' expected, but '{' found
@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.
tag edited ! i get this error: ';' expected, but '{' found
@hosseinvalizadeh See the updated edit, you need an = sign in C, and cannot miss the inner braces.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.