C++ tination type”(即本例中的short)?为什么在重新解释转换括号内没有“short”?未签名字符*p=重新解释转换(&a)?@user3811839:为什么?&a已经是一个short*。我们想要的是访问构成该short的各个字节。我理解:)

C++ tination type”(即本例中的short)?为什么在重新解释转换括号内没有“short”?未签名字符*p=重新解释转换(&a)?@user3811839:为什么?&a已经是一个short*。我们想要的是访问构成该short的各个字节。我理解:),c++,arrays,byte,bit,C++,Arrays,Byte,Bit,tination type”(即本例中的short)?为什么在重新解释转换括号内没有“short”?未签名字符*p=重新解释转换(&a)?@user3811839:为什么?&a已经是一个short*。我们想要的是访问构成该short的各个字节。我理解:)我以为尖括号内的类型应该是您的“目标类型”(即本例中的short)在C++中,从非活动的联合成员中读取是未定义的行为。@ BeoviigTy,这是有趣的。请在阅读中,在C++中,从不活动的联合成员中读取是未定义的行为。@ Beovigt Ty,


tination type”(即本例中的short)?为什么在重新解释转换括号内没有“short”?未签名字符*p=重新解释转换(&a)?@user3811839:为什么?
&a
已经是一个
short*
。我们想要的是访问构成该short的各个字节。我理解:)我以为尖括号内的类型应该是您的“目标类型”(即本例中的short)在C++中,从非活动的联合成员中读取是未定义的行为。@ BeoviigTy,这是有趣的。请在阅读中,在C++中,从不活动的联合成员中读取是未定义的行为。@ Beovigt Ty,这是有趣的阅读。非常感谢。
char c[2];
c[0] = 2;
c[1]= 2;
short a = short(c[0]);
short a;
unsigned char * p = reinterpret_cast<unsigned char *>(&a);

p[0] = 2;
p[1] = 2;
short a = c[0];
short a = (short)(c[0] + (c[1] << 8));
char c[] = { whatever, from whatever source };
short s;
memcpy(&s, c + offset, sizeof (short));
short s = *(short*)(c + offset); // C-style cast, equivalent to C++ reinterpret_cast
short s = (c[offset + 1] << 8) | (uint8_t)c[offset]; // little-endian
short s = (c[offset] << 8) | (uint8_t)c[offset + 1]; // big-endian
union
{
    char c[2];
    short s;
}
c[0] = 2;
c[1] = 2;

short a = s;