函数问题
memcpy()
这个函数是什么意思?
问题点数:0、回复次数:13Top
1 楼hnjl()回复于 2005-04-02 12:49:29 得分 0
函数原形是这样的:
void *memcpy( void *dest, const void *src, size_t count );
Return Value
memcpy returns the value of dest.
Parameters
dest
New buffer
src
Buffer to copy from
count
Number of characters to copy
Remarks
The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
Example
/* MEMCPY.C: Illustrate overlapping copy: memmove
* handles it correctly; memcpy does not.
*/
#include <memory.h>
#include <string.h>
#include <stdio.h>
char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/* 1 2 3 4 5
* 12345678901234567890123456789012345678901234567890
*/
void main( void )
{
printf( "Function:\tmemcpy without overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );
printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}
Output
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
Function: memmove with overlap
Source: quick brown fox jumps over the lazy dog
Destination: brown fox jumps over the lazy dog
Result: The quick quick brown fox jumps over the lazy dog
Length: 49 characters
Function: memcpy with overlap
Source: quick brown dog jumps over the lazy fox
Destination: brown dog jumps over the lazy fox
Result: The quick quick brown dog jumps over the lazy fox
Length: 49 characters
Top
2 楼du51(郁郁思扬)回复于 2005-04-02 12:49:55 得分 0
memory copy....Top
3 楼kobefly(科比--网络学习中)回复于 2005-04-02 12:51:04 得分 0
函数名: memcpy
功 能: 从源source中拷贝n个字节到目标destin中
用 法: void *memcpy(void *destin, void *source, unsigned n);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char src[] = "******************************";
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
char *ptr;
printf("destination before memcpy: %s\n", dest);
ptr = memcpy(dest, src, strlen(src));
if (ptr)
printf("destination after memcpy: %s\n", dest);
else
printf("memcpy failed\n");
return 0;
}
Top
4 楼hohoxcn()回复于 2005-04-02 12:58:11 得分 0
void *memcpy( void *dest, const void *src, size_t count )
存储器内容拷贝,将src指向的长count的存储器内容拷贝到dest指向的位置。Top
5 楼cppprogramlover(爱相随—)回复于 2005-04-07 06:19:35 得分 0
upTop
6 楼MagicCarmack(MagiC++)回复于 2005-04-07 10:24:52 得分 0
copymoney
moneycopy(memcpy)Top
7 楼sky911911(assda)回复于 2005-04-07 10:56:13 得分 0
查找一个就可以了 呵呵...Top
8 楼kobefly(科比--网络学习中)回复于 2005-04-07 11:17:43 得分 0
内存的按位拷贝Top
9 楼junmayang(笨猪)回复于 2005-04-08 09:58:48 得分 0
内存拷贝,优点是高速?Top
10 楼pcboyxhy(-273.15℃)回复于 2005-04-08 10:17:13 得分 0
高速不高速还要看库的实现Top
11 楼ccplus(++cc)回复于 2005-04-08 11:25:09 得分 0
成员拷贝!Top
12 楼shankeke(Cer)回复于 2005-04-08 11:55:42 得分 0
从源中拷贝n个字节到目标d中Top
13 楼ptb1212(天狗)回复于 2005-04-08 12:51:47 得分 0
内存拷贝函数Top




