memcpy和strlen函数的实现

#include <stdio.h>

#include <stdlib.h>			//malloc()函数

typedef unsigned int size_t;



size_t my_strlen(const char * str)

{

	const char *sc = NULL;

	if(str == NULL)

		return 0;

	for(sc = str;*sc != '\0';sc++)

	{

		/*  do nothing */

	}				

	return sc - str;

}



void * my_memcpy(void *pDst,const void *pSrc,size_t len)

{

	if((pDst == NULL)&&(pSrc == NULL))	//判断指针是否为空

		return NULL;

	char *tmp = (char *)pDst;

	char *s = (char *)pSrc;



	while(len--)

		*tmp++ = *s++;

	return pDst;

}



int main()

{

	char *src = "hello world";

	int count = my_strlen(src);

	printf("count = %d\n",count);

	char *dst = (char *)malloc(count + 1);	//申请空间

	

	my_memcpy(dst,src,count);

	*(dst+count) = '\0';

	printf("%s\n",dst);

    return 0;

}


执行结果:

memcpy和strlen函数的实现


 

你可能感兴趣的:(函数)