标题:昨天看的,拿来分享,呵呵
只看楼主
蕙雨星
Rank: 2
等 级:论坛游民
帖 子:25
专家分:24
注 册:2011-2-23
结帖率:100%
已结贴  问题点数:20 回复次数:4 
昨天看的,拿来分享,呵呵
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
包含:#include <string.h>;
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
memset
包含:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>;
功能:把buffer所指内存区域的前count个字节设置成字符c。
说明:返回指向buffer的指针

#include <stdio.h>
#include <string.h>
int main(void)
{
   char src[] = "******************************";
   char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
   char *ptr;
   printf("destination before memcpy: %s\n", dest);
   ptr =(char*) memcpy(dest, src, strlen(src));
   if (ptr)
      printf("destination after memcpy:  %s\n", dest);
   else
      printf("memcpy failed\n");
   return 0;
}
搜索更多相关主题的帖子: include count 
2011-02-24 13:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
else
      printf("memcpy failed\n");

想当然的吧???
2011-02-24 13:59
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:0 
把src全部复制的话就跟strcat()一样了。
其他的话,谢谢分享了!

   唯实惟新 至诚致志
2011-02-24 14:01
huangapple
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:545
专家分:1790
注 册:2010-12-30
得分:20 
#include <stdio.h>
#include <string.h>
int main(void)
{
   char src[] = "******************************";
   char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
   char *ptr = NULL;//这样比较好,但在这边没差啦
   printf("destination before memcpy: %s\n", dest);
   ptr =(char*) memcpy(dest, src, strlen(src));
   if (ptr) //这样很。。。。好像不管怎么样都不会返回0;memcpy在MSDN里的返回值是这样的 Return Valuememcpy returns the value of dest.

      printf("destination after memcpy:  %s\n", dest);
   else
      printf("memcpy failed\n");
   return 0;
}

MSDN对这个函数的解释是这样的
memcpy
Copies characters between buffers.

Routine Required Header
memcpy <memory.h> or <string.h>


void *memcpy( void *dest, const void *src, size_t count );
Parameters
dest
New buffer
src
Buffer to copy from
count
Number of characters to copy
Libraries
All versions of the C run-time libraries.

Return Values
memcpy returns the value of dest.

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

勤能补拙,熟能生巧!
2011-02-24 14:15
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
得分:0 
感谢楼主

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-02-24 17:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-332707-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.122768 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved