• memcpy函数-C语言


    原型:  void *memcpy(void *dest, const void *src, size_t n);

    #include<string.h>

    功能:从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中

    Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination.

    The function does not check for any terminating null character in source - it always copies exactly num bytes.

    size_t is an unsigned integral type.

    返回值:.src和dest所指内存区域不能重叠,函数返回指向dest的指针

    strcpy和memcpy区别如下:

    1.复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。

    2.用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

    3.复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符""才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。

     

      1 //#define FIRST_DEMO
      2 //#define SECOND_DEMO
      3 //#define THIRD_DEMO
      4 #define MYMEMCPY      
      5 //#define FORTH_DEMO
      6 
      7 #ifdef FIRST_DEMO
      8 #include <stdio.h>
      9 #include <conio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 int main(void)
     13 {
     14     char *s="Golden Global View";
     15     char d[20];
     16     system("cls");
     17     memcpy(d,s,(strlen(s)+1));  //将 s的字符串复制到数组d中
     18     printf("%s
    ",d);
     19     getch();
     20     return 0;
     21 }
     22 #elif defined SECOND_DEMO
     23 #include <stdio.h>
     24 #include <conio.h>
     25 #include <stdlib.h>
     26 #include <string.h>
     27 int main(void)
     28 {
     29     char *s="Golden Global View";
     30     char d[20];
     31     system("cls");
     32     /*从第14个字符(V)开始复制,连续复制4个字符(View)*/
     33     memcpy(d,s+14*sizeof(char),4*sizeof(char));
     34     d[4]='';   //这个语句若不加,输出的字符中有未初化的字符,显示乱码。
     35     printf("%s
    ",d);
     36     getch();
     37     return 0;
     38 }
     39 #elif defined THIRD_DEMO
     40 #include <stdio.h>
     41 #include <conio.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 int main(void)
     45 {
     46     char src[] = "******************************";
     47     char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6";
     48     printf("destination before memcpy: %s
    ", dest);
     49     memcpy(dest,src,strlen(src));
     50     printf("destination after memcpy: %s
    ", dest);
     51     getch();
     52     return 0;
     53 }
     54 #elif defined MYMEMCPY
     55 #include <stdio.h>
     56 #include <conio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 void *mymemcpy(void *dest,const void *src,size_t count);
     60 int main(void)
     61 {
     62     char src[] = "******************************";
     63     char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6";
     64     printf("destination before mymemcpy: %s
    ", dest);
     65     mymemcpy(dest,src,strlen(src));
     66     printf("destination after mymemcpy: %s
    ", dest);
     67     getch();
     68     return 0;
     69 }
     70 
     71 void *mymemcpy(void *dest,const void *src,size_t count)
     72 {
     73     char *ret=(char *)dest;
     74     char *dest_t=ret;
     75     const char *src_t=(char *)src;
     76     //append
     77 //     while(*dest_t!='')
     78 //     {
     79 //         dest_t++;
     80 //     }
     81     while(count--)
     82     {
     83         *dest_t++=*src_t++;
     84     }
     85     return ret;
     86 }
     87 #elif defined FORTH_DEMO
     88 #include <stdio.h>
     89 #include <conio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 struct  
     93 {
     94     char name[40];
     95     int age;
     96 }person,person_copy;
     97 int main(void)
     98 {
     99     char myname[]="Pierre de Fermat";
    100     printf("sizeof(myname)=%d
    ",sizeof(myname));
    101     printf("strlen(myname)=%d
    ",strlen(myname));
    102     memcpy(person.name,myname,strlen(myname)+1);
    103     person.age=48;
    104     memcpy(&person_copy,&person,sizeof(person));
    105     printf("person_copy :%s,%d
    ",person_copy.name,person_copy.age);
    106     getch();
    107     return 0;
    108 }
    109 #endif
  • 相关阅读:
    关于Netty4.x中文教程系列更新进度的说明和道歉
    Netty4.x中文教程系列(四) ChannelHandler
    Netty4.x中文教程系列(三) Hello World !详解
    Netty4.x中文教程系列(二) Hello World !
    Netty4.x中文教程系列(一) 目录及概述
    【推荐】HTML5 UI框架 推荐
    【转载】【JQuery学习】jQuery插件开发
    前端与后端的数据交互(jquery ajax+python flask)
    【JS教程32】ajax
    【JS教程31】json
  • 原文地址:https://www.cnblogs.com/shikamaru/p/7645583.html
Copyright © 2020-2023  润新知