如何定义等于 16 字节的类型

How can I define a type that is equal to 16 byte

我想定义一个等于 16 字节数组的类型。诸如此类:

typedef uint8_t[16] mynewType;

但我收到错误消息。我该如何定义这种类型?

我在这一行遇到了几个错误,例如:

missing ';' before '['  
empty attribute block is not allowed    
missing ']' before 'constant'
'constant'  

刚刚

typedef uint8_t mynewType [16];

类似于数组变量:

typedef uint8_t mynewType[16];

typedef 类似于声明,但前面多了一个 typedef

所以如果

uint8_t my_array[16]; 

声明一个新数组。

typedef uint8_t my_array[16]; 

使my_array成为这样一个数组的类型。

typedef unsigned char mynewType [16];

是在任何平台上分配16字节的可移植方式; CHAR_BIT 不一定 必须是 8.

您可以使用具有该大小数组字段的结构。但是您仍然需要设置各个字节值。如果你想以不同的方式访问不同的内存块,你也可以使用联合。

// simple data structure of 16 bytes
struct pack_16 {
    uint8_t data[16];
}
// sizeof(pack_16) == 16

// multi type access of 16 bytes
union multi_pack_16 {
    uint8_t  uint_8[16];
    uint16_t uint_16[8];
    uint32_t uint_32[4];
    uint64_t uint_64[2];
}
// sizeof(multi_pack_16) == 16

此外,根据您的编译器,uint128_t 数据类型可能定义为 16 字节大小。