首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

strcpy函数解决方法

2012-02-24 
strcpy函数char*str(char*)MALLOC(2)strcpy(str,12 )FREE(str)这段代码会出现错误吧,为什么?[解决办

strcpy函数
char   *str   =   (char   *)   MALLOC(2);
strcpy(str,   "12 ");
FREE(str);
这段代码会出现错误吧,为什么?

[解决办法]
应该malloc(3),字符串最后还有一个0
[解决办法]
char *strcpy(char *strDest,const char *strSrc)
{
assert((strDest!=NULL)&&(strSrc !=NULL))
char *address = strDest;
while((*strDest++ = *strSrc)!= '\0 ')
NULL;
return address;
}

所以要碰到0才结束copy,但是那个时候你的str已经溢出了

热点排行