简体   繁体   中英

How to initialize an unsigned char array from a string literal?

I have a struct with an unsigned char[16] field that I'd like to initialize to zeros. The following (strongly simplified) code compiles fine with clang (OS X):

struct GUID {
    unsigned char bytes[16];
    GUID() : bytes("\0\0\0\0""\0\0\0\0""\0\0\0\0""\0\0\0") {};
}

Note I use 15 \\0 s because the 16th is the zero terminator of the string literal, and clang complains if you initialize a string with too many bytes.

Now, when I try to compile with GCC 4.5.3 (cygwin), I get:

error: incompatible types in assignment of 'const char [16]' to 'unsigned char [16]'

Why doesn't it work, and how can I make it work? (I could obviously loop through the array in the constructor, but I'd like to use the initialization list if possible, and I'd like to understand why it works in one compiler, but not the other.)

A simple bytes() should be sufficient in this case. You could also consider bytes { 0,0,0,0.... } .

Also, use std::array , not T[] . Only fools use T[] when they could use std::array .

Since we're dealing with POD its sufficient to explicitly construct bytes, which will result in the corresponding memory being 'zeroed':

struct GUID 
{
    unsigned char bytes[16];
    GUID() : bytes(){}
};

It's probably worth noting that if you didn't explicitly construct bytes in the initialization list, it would be left uninitialized

struct GUID 
{
    unsigned char bytes[16];
    GUID(){};
};

If the member variable were not a POD but instead a member object then instead of being left uninitialized it would call its default constructor.

在GUID构造函数中,您可以使用memset(bytes,0,sizeof(bytes));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM