欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

memcpy一种实现方法

程序员文章站 2022-06-19 10:39:30
1 #include 2 #include 3 4 void* memncpy(void* dest, const void* src, int count){ 5 int i=0; 6 //void *init_dest = dest; 7 char* dest1 = (char*)dest; 8... ......
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void* memncpy(void* dest, const void* src, int count){
 5     int i=0;
 6     //void *init_dest = dest;
 7     char* dest1 = (char*)dest;
 8     const char* src1 = (char*)src;
 9     for(;i<count;i++){
10         *dest1=*src1;
11         dest1++;
12         src1++;
13     }
14     return dest;
15 }
16 int main(void){
17     int num=3;
18     char str1[8] ="abcdefg";
19     char* str2 = (char*)malloc(8);
20      //str1= "abcdefg"; // error: incompatible types when assigning to type ‘char[8]’ from type ‘char *
21      str2= "hijklmn";
22     printf("1str1:%s \n",str1);
23     memncpy(str1,str2,num);
24     printf("2str1: %s \n",str1);
25     return 0;
26 }