unsigned shot类型的数组赋值的问题
有两个unsigned shot类型的数组,想将一个数组的所有内容复制到另外一个数组中(不想用for循环之类的),有没有如strcpy之类的方法? 或用memset? 问题点数:30、回复次数:5Top
1 楼kunp(一天一小步)回复于 2004-09-01 14:07:21 得分 20
memcpy就可以了。看看例子:
#include <iostream>
using namespace std;
int main()
{
unsigned short s1[] = { 2, 3, 4, 5, 6, 7};
unsigned short s2[] = { 1, 1, 1, 1, 1, 1};
int ix = 0;
cout << "s1:\n";
for(ix = 0; ix < sizeof(s1)/sizeof(unsigned short); ix++)
{
cout << s1[ix] << endl;
}
cout << "s2:\n";
for(ix = 0; ix < sizeof(s1)/sizeof(unsigned short); ix++)
{
cout << s2[ix] << endl;
}
memcpy(s1, s2, sizeof(s1));
cout << "after memcpy" << endl;
cout << "s1:\n";
for(ix = 0; ix < sizeof(s1)/sizeof(unsigned short); ix++)
{
cout << s1[ix] << endl;
}
cout << "s2:\n";
for(ix = 0; ix < sizeof(s1)/sizeof(unsigned short); ix++)
{
cout << s2[ix] << endl;
}
return 0;
}Top
2 楼oo(为了名副其实,努力学习oo技术ing)回复于 2004-09-01 14:07:25 得分 5
memcpyTop
3 楼yanghuajia(我要抢分)回复于 2004-09-01 14:09:49 得分 0
memcpy --linux
Top
4 楼bejesus(天塌下来)回复于 2004-09-01 14:26:56 得分 0
谢谢!我想也是memcpy啦!
但如果这两参数都是unsigned short型数组的指针呢? 这时有什么办法能知道源数组的大小?Top
5 楼lifeixiao(李飞笑)回复于 2004-09-02 16:10:52 得分 5
unsigned short a[10],b[100];
int a_len,b_len;
a_len=sizeof(b)/sizeof(unsigned short);
b_len=sizeof(b)/sizeof(unsigned short);
Top




