Home
Aptitude
Categories


Home » C solved programs » C string programs

Write your own memcpy() function in C



Here, we are going to learn how to write your own memcpy() function to copy blocks of memory from one location to another?
Submitted by IncludeHelp, on December 06, 2018

As we have discussed in the previous post that memcpy() is a library function of "string.h" in C language and it is used to copy blocks of memory from one location to another.

Read more: memcpy() function in C

Here, we are going to create our own "memcpy()" function...

Function prototype:

    myMemCpy(void* target, void* source, size_t n);

It will copy n bytes of the source to target.

Function definition:

//memcpy() Implementation, name: myMemCpy()
void myMemCpy(void* target, void* source, size_t n){
	int i;
	//declare string and type casting 
	char *t = (char*)target;
	char *s = (char*)source;
	//copying "n" bytes of source to target
	for(i=0;i<n;i++)
		t[i]=s[i];
}

Example 1) Copying a string to another (all bytes of a string to another)

#include <stdio.h>
#include <string.h>
#define MAX_CHAR 50

//memcpy() Implementation, name: myMemCpy()
void myMemCpy(void* target, void* source, size_t n){
	int i;
	//declare string and type casting 
	char *t = (char*)target;
	char *s = (char*)source;
	//copying "n" bytes of source to target
	for(i=0;i<n;i++)
		t[i]=s[i];
}

int main(){
	char str1[MAX_CHAR] = "Hello Wold!";
	char str2[MAX_CHAR] = "Nothing is impossible";

	printf("Before copying...\n");
	printf("str1: %s\n", str1);
	printf("str2: %s\n", str2);

	//copying all bytes of str2 to str1
	myMemCpy(str1, str2, strlen(str2));

	printf("After copying...\n");
	printf("str1: %s\n",str1);
	printf("str2: %s\n",str2);

	return 0;
}

Output

Before copying...
str1: Hello Wold!
str2: Nothing is impossible
After copying...
str1: Nothing is impossible
str2: Nothing is impossible



Example 2) Copying some of the bytes from a byte array to another array

#include <stdio.h>
#include <string.h>
#define MAXLEN 11
//function to print array 

void printArray(unsigned char str[], int length){
	int i;
	for(i=0; i<length;i++)
		printf("%02X ", str[i]);
	printf("\n");
}

//memcpy() implementation, name: myMemCpy()
void myMemCpy(void* target, void* source, size_t n){
	int i;
	//declare string and type casting
	char *t = (char*)target;
	char *s = (char*)source;

	//copying "n" bytes of source to target
	for(i=0;i<n;i++)
		t[i]=s[i];
}

int main(){
	unsigned char arr1[MAXLEN] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0x95};
	unsigned char arr2[MAXLEN] = {0};

	printf("Before copying...\n");
	printf("arr1: "); printArray(arr1,strlen(arr1));
	printf("arr2: "); printArray(arr2,strlen(arr2));

	//copying 5 bytes of arr1 to arr2
	myMemCpy(arr2, arr1, 5);
	printf("After copying...\n");
	printf("arr1: "); printArray(arr1,strlen(arr1));
	printf("arr2: "); printArray(arr2,strlen(arr2));
	
	return 0;
}

Output

Before copying...
arr1: 10 20 30 40 50 60 70 80 90 95
arr2:
After copying...
arr1: 10 20 30 40 50 60 70 80 90 95
arr2: 10 20 30 40 50





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.