# 初学一个简单的有关问题。多谢

www.myexceptions.net  网友分享于：2013-03-03  浏览：1次

------解决方案--------------------
Template data1;
Template data2;
Template data3;
struct data
{
data1 aaa[4];
data2 bbb[6];
data3 ccc;
};

------解决方案--------------------

#include <iostream>
using namespace std;
struct SaveNumbers
{
int nArray[5];
float fArray[5];
char cArray[5];
}s1;

template <class T>
void Show(T typeAyyay[],int count)
{
int i;
for(i=0;i <count;i++)
{
cout < <typeAyyay[i] < < " ";
}
cout < <endl;
}

int main()
{
s1.nArray[0]=1;
s1.nArray[1]=2;
s1.nArray[2]=3;
s1.nArray[3]=4;
s1.nArray[4]=5;

s1.fArray[0]=1.1;
s1.fArray[1]=1.2;
s1.fArray[2]=1.3;
s1.fArray[3]=1.4;
s1.fArray[4]=1.5;

s1.cArray[0]= 'a ';
s1.cArray[1]= 'b ';
s1.cArray[2]= 'c ';
s1.cArray[3]= 'd ';
s1.cArray[4]= 'e ';

Show(s1.nArray,5);
Show(s1.fArray,5);
Show(s1.cArray,5);

return 0;
}
------解决方案--------------------
char aaa[4]=;int bbb[6];short int ccc[8];
char ddd[1000];
int len = 0, offset = 0;

len = sizeof(aaa);
memcpy(ddd + offset, aaa, len);
offset += len;

len = sizeof(bbb);
memcpy(ddd + offset, bbb, len);
offset += len;

len = sizeof(ccc);
memcpy(ddd + offset, ccc, len);
offset += len;