C语言中往缓存写入结构体的方法


C语言中往缓存写入结构体的方法

typedef struct Cmytype
 {
  int a;
  char b;
 };
main()

 char buffer[100];
 Cmytype data1,data2;


 data1.a = 100;
 data1.b = 'a';
 data2.a = 119;
 data2.b = 'b';

//写入缓存
 ((Cmytype *)buffer)[0] = data1;
 ((Cmytype *)buffer)[1] = data2;

 

//检验写入的正确性

    Cmytype result1 = ((Cmytype *)buffer)[0];
 Cmytype result2 = ((Cmytype *)buffer)[1];

    cout<<result1.a<<endl<<result1.b<<endl;
 cout<<result2.a<<endl<<result2.b<<endl;
}

相关内容