歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言中往緩存寫入結構體的方法

C語言中往緩存寫入結構體的方法

日期:2017/3/1 10:04:25   编辑:Linux編程

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;
}

Copyright © Linux教程網 All Rights Reserved