strcpy函數實現(考慮內存摺疊)

char *my_strcpy(char *dst, const char *src, size_t cnt)
{
	if (nullptr == dst || nullptr == src || cnt == 0)
		return nullptr;
	char *temp1 = dst;
	const char *temp2 = src;
	if (dst <= src || src + cnt <= dst)
	{
		while (cnt--)
			*temp1++ = *temp2++;
	}
	if (dst > src)
	{
		temp1 += cnt;
		temp2 += cnt;
		while (cnt--)
			*temp1-- = *temp2--;
	}
	return dst;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章