User defined function of memcpy().
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void own_memcpy(void *to, void *from, int n)
{
while(n- -)
{
*(char*)to = *(char*)from;
to = (char*)to + 1;
from = (char*)from + 1;
}
}
int main()
{
char *ptr = NULL;
char *ptr1 = NULL;
ptr = (char*)malloc(25);
ptr1 = (char*)malloc(25);
printf(“Enter value of ptr nad ptr1\n”);
gets(ptr);
gets(ptr1);
/* Case of overlapping: Destination pointer is greater than source */
own_memcpy(ptr+5, ptr+1, 10);
printf(“ptr after memcpy\n”);
puts(ptr);
/* Case2: Destination pointer is less than source */
own_memcpy(ptr1+1, ptr1+5, 10);
printf(“ptr1 after memcpy\n”);
puts(ptr1);
free(ptr);
free(ptr1);
return 0;
}
Output:
$ ./a.exe
Enter value of ptr nad ptr1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ptr after memcpy
ABCDEBCDEBCDEBCPQRSTUVWXYZ
ptr1 after memcpy
AFGHIJKLMNOLMNOPQRSTUVWXYZ