C/C中的memcpy()++

memcpy()用于将内存块从一个位置复制到另一个位置。它是在 一串H

null
// Copies "numBytes" bytes from address "from" to address "to"void * memcpy(void *to, const void *from, size_t numBytes);

下面是一个示例C程序,展示了memcpy()的工作原理。

C

/* A C program to demonstrate working of memcpy */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[] = "Geeks" ;
char str2[] = "Quiz" ;
puts ( "str1 before memcpy " );
puts (str1);
/* Copies contents of str2 to str1 */
memcpy (str1, str2, sizeof (str2));
puts ( "str1 after memcpy " );
puts (str1);
return 0;
}


输出:

str1 before memcpy Geeksstr1 after memcpy Quiz

笔记: 1) memcpy()不检查溢出或 2) 当源地址和目标地址重叠时,memcpy()会导致问题。

memmove() 是另一个可以很好地处理重叠的库函数。 编写自己的memcpy()和memmove() 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享