31 - memmove()函数

1 函数原型

memmove():移动内存块,函数原型如下:

void * memmove ( void * destination, const void * source, size_t num );

cstring库描述如下:

Move block of memory
1. Copies the values of num bytes from the location pointed by source to the memory block pointed by destination.
2. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
3. The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
4. The function does not check for any terminating null character in source - it always copies exactly num bytes.
5. To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
  1. memmove()函数:
    (1)从source指向的内存块直接复制num个字节的值到destination指向的内存块;
  2. memmove()函数和memcpy()函数都用于内存拷贝,区别在于:
    (1)memmove()函数能够正确处理内存重叠的情况,而memcpy()函数不能。

2 参数

memmove()函数有三个参数source、destination和num:

  1. source是指向源内存块的指针,类型为void*;
  2. destination是指向目标内存块的指针,类型为void*;
  3. num是要复制的字符数,类型为size_t。

cstring库描述如下:

destination
1. Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

source
1. Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

num
1. Number of bytes to copy.
2. size_t is an unsigned integral type.

3 返回值

memmove()函数的返回值类型为void*型:

  1. 返回指向目标内存块的指针destination。
    cstring库描述如下:
1. destination is returned.

4 示例

示例代码如下所示:

int main() {
   //
   char str[] = "abcdefghijklmn";
   //
   printf("移动前:%s\n", str);
   //
   memmove(str + 6, str + 4, 6);
   //
   printf("移动后:%s\n", str);
   //
   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述
代码及运行结果分析如下:

  1. 期望用"efghij"替换"ghijkl",替换成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值