超级版主
注册日期: 04-03
帖子: 18592
精华: 36
现金: 249466 标准币
资产: 1080358888 标准币
|
回复: 【转帖】bug分析
strcpy()赋值和直接赋值的区别
在路上 11:32:01
struct stUserListNode
{
char userName[10];
unsigned int ip;
unsigned short port;
};
在路上 11:32:59
如何把一个CString赋值给userName
海阔天高 11:33:30
strcpy
在路上 11:43:38
stUserListNode *NodeMsg = new stUserListNode;
strcpy(NodeMsg.userName, strName);
在路上 11:43:46
哥们这样不行
在路上 11:43:58
error C2228: left of '.userName' must have class/struct/union type
鱼绕落水花 11:45:26
lstrcpy(对象.userName,CString变量.GetBuffer(CString变量.Getlength());
海阔天高 11:45:27
strcpy(NodeMsg.userName, strName.GetBuffer());
酱菜Pickle 11:59:21
NodeMsg是指针 请用-> , strName.getbuffer是正解
在路上 12:11:52
strcpy( NodeMsg->userName, strName.GetBuffer(strName.GetLength()))
在路上 12:11:57
OK了
在路上 12:12:30
NodeMsg->userName = strName.GetBuffer(strName.GetLength());
一开始我用这个不行
在路上 12:12:39
两者区别主要是什么
酱菜Pickle 12:58:46
GetBuffer是没有参数的函数
酱菜Pickle 12:59:18
他只是获取CString里面数据区的char* 首地址
酱菜Pickle 13:00:44
所以你 不知直接用NodeMsg->userName拷贝地址,必须申请另外的空间存放,不然你的strName一析构 你的NodeMsg->userName就变成野指针了
酱菜Pickle 13:00:57
所以你 不能直接用NodeMsg->userName拷贝地址,必须申请另外的空间存放,不然你的strName一析构 你的NodeMsg->userName就变成野指针了
在路上 13:20:49
谢谢!
__________________
借用达朗贝尔的名言:前进吧,你会得到信心!
[url="http://www.dimcax.com"]几何尺寸与公差标准[/url]
|