将数组中元素赋值给struct中元素(类型需一致,否则保持默认值),若数组元素少,struct中未被赋值的保持默认值。若数组元素多,对应位置的元素会赋值给struct。
#include<iostream> struct MyStruct { short n1; //默认0 short n2; }; int main() { uint16_t nArray[4] = { 0,1,2,3 }; //uint16_t是unsigned short,可以给short型赋值 MyStruct *s_My = (MyStruct*)nArray; //nArray默认指向首地址,所以n1=0,n2=1 s_My = (MyStruct*)(nArray + 2); //n1=2,n2=3 return 0; }
【值得一看】
[C++] 结构体Struct类型和变量定义_丶布布的博客-CSDN博客
标签:short,struct,MyStruct,c++,n1,nArray,赋值 From: https://www.cnblogs.com/xixixing/p/17721752.html