求解:为什么不能用memcpy将一个unsigned char[10]的内容拷贝到unsigned char*中? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么不能用memcpy将一个unsigned char[10]的内容拷贝到unsigned char*中? --答案:你程序写错了。 楼上的回答真坏哦你用unsigned char 数组吧估计你是直接拿一个指针放进去做操作的吧 我的代码如下:char* Msg_Id;unsigned char[10] Content;memcpy(Msg_Id, Content, sizeof(Content); <---这一句代码就会运行出错。 我的代码如下:char* Msg_Id;unsigned char[10] Content;memcpy(Msg_Id, Content, sizeof(Content); <---这一句代码就会运行出错。你的char* Msg_Id;只是个指针没有空间来放你要放的东西所以你可以1 char Msg_Id[10];2 char*Msg_Id; Msg_Id=(char*)malloc(10); ... free(Msg_Id);用完要free的 char* Msg_Id; // 没有分配内存!unsigned char[10] Content;memcpy(Msg_Id, Content, sizeof(Content); Msg_Id在使用前应该先指向块有效的空间譬如:char* Msg_Id;unsigned char[10] Content;char buf[10];Msg_Id=buf;memcpy(Msg_Id, Content, sizeof(Content); 谢谢各位大哥指点迷津了。不过说的方法都是Msg_Id有固定的长度的,如果没有固定长度怎么处理呢?如果分配太长的空间又比较浪费 这样写:char* Msg_Id;Msg_Id=new char[10];unsigned char[10] Content;memcpy(Msg_Id, Content, sizeof(Content); 用new来分配就可以了不过记得要delete这样的话,你要多大就分配多大咯 VC6.0下使用DrawTextEx 临时表是否可以作水晶报表的数据源?记录集对象作为水晶报表的数据源? 如何动态往模态窗口的static框输入文本? TAPI的问题,求助 这是个什么错误啊~ 第一次做控件不明白 给点指点啊 如何用OpenGL画一个固定方向的面 vc中用什么能完成以下功能? 为何在对话框种的组合框GetFocus() == 组合框得不到焦点 统计当前的modem速率,就象网络蚂蚁中一样,100分 终于挤进了前50名,给大家送分以示庆贺 请问如何锁文件夹 急问:用VC6写的一个Add-In程序,如何进行调试??
--答案:你程序写错了。
char* Msg_Id;
unsigned char[10] Content;
memcpy(Msg_Id, Content, sizeof(Content); <---这一句代码就会运行出错。
char* Msg_Id;
unsigned char[10] Content;
memcpy(Msg_Id, Content, sizeof(Content); <---这一句代码就会运行出错。你的char* Msg_Id;只是个指针没有空间来放你要放的东西
所以你可以
1 char Msg_Id[10];
2 char*Msg_Id;
Msg_Id=(char*)malloc(10);
...
free(Msg_Id);
用完要free的
unsigned char[10] Content;
memcpy(Msg_Id, Content, sizeof(Content);
譬如:
char* Msg_Id;
unsigned char[10] Content;
char buf[10];
Msg_Id=buf;
memcpy(Msg_Id, Content, sizeof(Content);
不过说的方法都是Msg_Id有固定的长度的,如果没有固定长度怎么处理呢?如果分配太长的空间又比较浪费
char* Msg_Id;
Msg_Id=new char[10];
unsigned char[10] Content;
memcpy(Msg_Id, Content, sizeof(Content);