I am trying to create a char* strcpy(char* dest, const char*) function. This is what I have so far:
char* mystrcpy(char *dest, const char *src)
{
char* tempsrc = (char*) src;
int length = strlen(src);
char tempdest[length];
for(int i = 0; i < length;i++)
{
tempdest[i]= tempsrc[i];
}
tempdest[length] = '\0';
return dest = tempdest;
}
It returns the correct dest variable, so it can be saved in the main method, but if the return isn't saved, the *dest in the main method isn't changed like I would want it to.
How do I change the dest* pointer within strcpy?
[EDIT]
Here is the full method I have been using to test this
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv){
char* source = "stringone";
char* dest = "stringtwo";
char* mystrcpy(char *dest, const char *src);
printf("Before src: %s\n", source);
printf("Before dest: %s\n", dest);
mystrcpy(dest, source);
printf("After src: %s\n", source);
printf("After dest: %s\n", dest);
}
char* mystrcpy(char *dest, const char *src)
{
int length = strlen(src);
for(int i = 0; i < length;i++)
{
printf("test\n");
dest[i] = src[i];
printf("test1\n");
}
dest[length] = '\0';
return dest;
}