Is this method of copying a string faster than copying each char individually? The idea of this code is that it could (but I am not sure if it is so) be faster to copy 8 bytes at once instead of a single byte. It is very un-safe but it seems to work somehow. Or it's just a bad idea?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* copy_string(char s[])
{
int len=strlen(s);
int i=0;
double *p=(double*)s;
double *copy=(double*)malloc(len+1);
while(i-len>8)
{
*copy=*(p++);
copy++;
i+=8;
}
char *p2=(char*)p;
char *c=(char*)copy;
while(i<len)
{
*c=*(p2++);
c++;
i++;
}
*c='\0';
return copy;
}
int main()
{
char s[]="GOODBYE SAFE WORLD!";
char *c=copy_string(s);
printf("%s",c);
return 0;
}