memcpy function usage

藏色散人
Release: 2019-10-24 17:41:05
Original
13346 people have browsed it

memcpy指的是C和C++使用的内存拷贝函数,函数原型为void *memcpy(void *destin, void *source, unsigned n);函数的功能是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中,即从源source中拷贝n个字节到目标destin中。

memcpy function usage

函数原型

void *memcpy(void *destin, void *source, unsigned n);
Copy after login

参数

destin-- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。

source-- 指向要复制的数据源,类型强制转换为 void* 指针。

n-- 要被复制的字节数。

返回值

该函数返回一个指向目标存储区destin的指针。

功能

从源source所指的内存地址的起始位置开始拷贝n个字节到目标destin所指的内存地址的起始位置中。 [2]

所需头文件

C语言:#include

C++:#include

示例:

作用:将s中的字符串复制到字符数组d中。

//memcpy.c
#include <stdio.h>
#include <string.h>
int main()
{
    char* s="GoldenGlobalView";
    char d[20];
    clrscr();
    memcpy(d,s,(strlen(s)+1));        //+1 是为了将字符串后面的&#39;\0&#39;字符结尾符放进来,去掉+1可能出现乱码
    printf("%s",d);
    getchar();
    return 0;
}
Copy after login

输出结果:Golden Global View

The above is the detailed content of memcpy function usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!