当前位置: 技术问答>linux和unix
紧急求助:数组拷贝!
来源: 互联网 发布时间:2015-12-08
本文导语: 如何将n个不等长的数组拷贝成一个数组,请帮忙详细实现; 如:char des[10*50]; const char src1[]={0x34,0x23,0x7b,0xbe,03e}; const char src2[]={0x31,0x23,0x7b,0x45,0xfe,0xbe,03e}; const char src3[]={0xe4,0x23,0x7b,}; 结果:des[]={0x34,0x23,0x7b,0xbe,03...
如何将n个不等长的数组拷贝成一个数组,请帮忙详细实现;
如:char des[10*50];
const char src1[]={0x34,0x23,0x7b,0xbe,03e};
const char src2[]={0x31,0x23,0x7b,0x45,0xfe,0xbe,03e};
const char src3[]={0xe4,0x23,0x7b,};
结果:des[]={0x34,0x23,0x7b,0xbe,03e,0x31,0x23,0x7b,0x45,0xfe,0xbe,03e,0xe4,0x23,0x7b};
如:char des[10*50];
const char src1[]={0x34,0x23,0x7b,0xbe,03e};
const char src2[]={0x31,0x23,0x7b,0x45,0xfe,0xbe,03e};
const char src3[]={0xe4,0x23,0x7b,};
结果:des[]={0x34,0x23,0x7b,0xbe,03e,0x31,0x23,0x7b,0x45,0xfe,0xbe,03e,0xe4,0x23,0x7b};
|
memcpy(des, src1, sizeof(src1));
memcpy(des+sizeof(src1), src2, sizeof(src2));
memcpy(des+sizeof(src1)+sizeof(src2), src3, sizeof(src3));
...
memcpy(des+sizeof(src1), src2, sizeof(src2));
memcpy(des+sizeof(src1)+sizeof(src2), src3, sizeof(src3));
...
|
一个一个memcpy
事先用sizeof确定大小
自己计算偏移量
事先用sizeof确定大小
自己计算偏移量
|
对 用memcpy