I am writing my own memcpy() function . I am copying source string to destination . While copying , an error occurs as 'segmentation fault' . I am using codeblocks. Can anyone explain why? Am I copying strings incorrectly?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char info;
struct node *next;
}mynode;
void mymemcpy(void*,const void*,size_t);
int main()
{
char *p="sonampulkit";
char *q=p+2;
mymemcpy(q,p,strlen(p)+1);
printf("\n final dest=%s ",q);
printf("\n final src=%s ",p);
return 0;
}
void mymemcpy(void* to,const void* from,size_t n)
{
char *src=(char*)from;
char *dest=(char*)to;
printf("source=%s",src);
printf("\ndestination=%s",dest);
printf("\nsize=%d",n);
int i=0;
for(i=0;i<n;i++)
{
printf("\ndest=%c",*(dest+i));
printf("\nsrc=%c",*(src+i));
// At below line , error occurred.
dest[i]=src[i];
}
return dest;
}
Error: Program received segmentation fault.