日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

c语言中memcpy函数_带有示例的C中的memcpy()函数

發(fā)布時(shí)間:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言中memcpy函数_带有示例的C中的memcpy()函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c語言中memcpy函數(shù)

memcpy()函數(shù) (memcpy() function)

memcpy() is a library function, which is declared in the “string.h” header file - it is used to copy a block of memory from one location to another (it can also be considered as to copy a string to another).

memcpy()是一個(gè)庫(kù)函數(shù),在“ string.h”頭文件中聲明-它用于將一個(gè)內(nèi)存塊從一個(gè)位置復(fù)制到另一個(gè)位置(也可以視為將字符串復(fù)制到另一個(gè)位置)。

Syntax of memcpy():

memcpy()的語法:

memcpy(void*str1, const void* str2, size_t n);

It copies n bytes of str2 to str1.

它將str2的 n個(gè)字節(jié)復(fù)制到str1中 。

Example 1) Copying a string to another (all bytes of a string to another)

示例1)將一個(gè)字符串復(fù)制到另一個(gè)(字符串的所有字節(jié)到另一個(gè))

#include <stdio.h> #include <string.h> #define MAX_CHAR 50int main(void) {char str1[MAX_CHAR] = "Hello World!";char str2[MAX_CHAR] = "Nothing is impossible";printf("Before copying...\n");printf("str1: %s\n",str1);printf("str2: %s\n",str2);//copying all bytes of str2 to str1memcpy(str1, str2, strlen(str2));printf("After copying...\n");printf("str1: %s\n", str1);printf("str2: %s\n", str2);return 0; }

Output

輸出量

Before copying... str1: Hello World! str2: Nothing is impossible After copying... str1: Nothing is impossible str2: Nothing is impossible .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Example 2) Copying some of the bytes from a byte array to another array

示例2)將某些字節(jié)從一個(gè)字節(jié)數(shù)組復(fù)制到另一個(gè)數(shù)組

#include <stdio.h> #include <string.h> #define MAXLEN 11//function to print array void printArray(unsigned char str[], int length){int i;for(i=0; i<length;i++)printf("%02X ", str[i]);printf("\n"); }int main(void) {unsigned char arr1[MAXLEN] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0x95};unsigned char arr2[MAXLEN] = {0};printf("Before copying...\n");printf("arr1: "); printArray(arr1, strlen(arr1));printf("arr2: "); printArray(arr2, strlen(arr2));//copying 5 bytes of arr1 to arr2memcpy(arr2, arr1, 5);printf("After copying...\n");printf("arr1: "); printArray(arr1, strlen(arr1));printf("arr2: "); printArray(arr2, strlen(arr2));return 0; }

Output

輸出量

Before copying... arr1: 10 20 30 40 50 60 70 80 90 95 arr2: After copying... arr1: 10 20 30 40 50 60 70 80 90 95 arr2: 10 20 30 40 50

翻譯自: https://www.includehelp.com/c-programs/memcpy-function-in-c-with-example.aspx

c語言中memcpy函數(shù)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的c语言中memcpy函数_带有示例的C中的memcpy()函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。