现在的位置: 首页 > 综合 > 正文

strcpy函数实现

2013年08月31日 ⁄ 综合 ⁄ 共 336字 ⁄ 字号 评论关闭
#include <iostream>
#include <assert.h>
using namespace std;

char *strcpy_mf(char *dst,const char *src)
{
	assert((dst != NULL)&&(src != NULL));
	char *temp = dst;
	while(*src != '\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
	return temp;
}

void main()
{
	char *s2 = "copy11";//正常
	//char *s2;//运行错误
	//char *s2 = "";//运行正确,输出空串
	char *s1 = (char *)malloc(strlen(s2)+1);
	strcpy_mf(s1,s2);
	cout << s1 <<endl;
}

抱歉!评论已关闭.