C标准库--memcpy、memmove函数

1、The memcpy function

1.1、Synopsis

#include <string.h>
void *memcpy(void *s1, const void *s2, size_t n);

1.2、Description

  The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undeined.

1.3、Returns

The memcpy function returns the value of s1.

1.4、Implementing

/* memcpy function */
#include <string.h>

void *(memcpy)(void *s1, const void *s2, size_t n)
{
	char *su1;
	const char *su2;

	for (sul = s1, su2 = s2; 0 < n; ++su1, ++su2; --n)
		*su1 = *su2;
	
	return (s1);
}

1.5、Using

2、The memcpy function

2.1、Synopsis

#include <string.h>
void *memmove(void *s1, const void *s2, size_t n);

2.2、Description

  The memmove function copies n characters from the object pointed toby s2 into the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n character thar does not overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1.

2.3、Returns

  The memmove function returns the value of s1.

2.4、Implementing

/* memmove function */
#include <string.h>

void * (memmove)(void *s1, const void *s2, size_t n)	/* copy char s2[n] to s1[n] safely */
{
	char *sc1;
	const char *sc2;
	
	sc1 = s1;
	sc2 = s2;
	if (sc2 < sc1 && sc1 < sc2 + n)
		for (sc1 += n, sc2 += n; 0 < n; --n)			/* copy backwards */
			*--sc1 = *--sc2;
	else
		for (; 0 < n; --n)								/* copy forwards */
			*sc1++ = *sc2++;

	return s1;
}

2.5、Using

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值