Question

i'd like to ask for a help. When i got this:

struct MyStruct
{
    unsigned char myBytes[5];
    MyStruct()
    {
        myBytes[0] = 0x89;
        myBytes[1] =  0x50;
        myBytes[2] =  0x4E;
        myBytes[3] =  0x47;
        myBytes[4] =  0x0D;
    }        
};

How to make it easies ? like myBytes = {0x89, 0x50, 0x4E, 0x47, 0x0D};

Was it helpful?

Solution

In C++11, you can do either of the following:

struct MyStruct
{
    unsigned char myBytes[5] = {0x89, 0x50, 0x4E, 0x47, 0x0D};     
};

// or...

struct MyStruct
{
    unsigned char myBytes[5];
    MyStruct() : myBytes{0x89, 0x50, 0x4E, 0x47, 0x0D}
    { }        
};

Otherwise, you've got the best method already.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top