Q:

Write your own memcpy() function in C

0

Write your own memcpy() function in C

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];
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now