C语言学习笔记---memcpy函数的用法

函数原型

void *memcpy(void*dest, const void *src, size_t n);

定义

const void *src 为不可变更源数据,

void *dest 为数据复制的“目的地”,

size_t n 为复制的数据长度

功能

由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始地址的空间内。

*(功能描述摘自 C函数之memcpy()函数用法_冀博-CSDN博客_memcpy函数

以 MSND Library memcpy函数描述的例子举例:

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );// string1[40] 第41个数据‘f’作为起始地址
   printf( "Destination:\t%s\n", string1 + 16 );// string1[16] 第17个数据‘d’作为起始地址

   memcpy( string1 + 16, string1 + 40, 3 );
   // 将string1中第41个数据作为起点,复制三个数据到string1中第17个数据作为起点的数据中
   //实质就是用‘fox’替换掉‘dog’

   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}

运行结果:

Function:      memcpy without overlap
Source:        fox
Destination:   dog jumps over the lazy fox
Result:        The quick brown fox jumps over the lazy fox
Length:        43 characters

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值