So, i am trying to implement my own strcpy to have a better understanding of how pointers work in C, and so far i am at a loss. I tried a lot of different approaches, tried using code from the net to maybe base mine on later, but even those wont work.
I believe i might be calling the function in the wrong way on the main method. I am passing a pointer of the variable to the function, so that the function can change the value of the variable and on the function call, i am giving the adress of the pointer so the method can get the correct data. At least i believe this is how it works, from what i could understand at any rate.
Here are the functions and the method calls:
Function call:
void my_strcpy( char *dest, const char* src ) {
dest = malloc(sizeof(src));
while ( src != '\0' ) {
*dest = *src;
src++;
dest++;
}
}
Main:
int main () {
const char* s = "Hello World";
char* test = 'd';
my_strcpy( &test, &s ); // Suposed to change test form d to Hello World.
printf("Changed String: " );
printf( "%c", test );
return 0;
}
PS: I tried using test as a string bigger than s to see if it was a memory allocation problem but it does not appear to be so. Any help is greatly apreciated as i have been stuck in this for hours...
PS: I've looked into the question and examples of how to change the variable and can do it using a simple int. But i cannot do it when using char*. Hence the post. I apologize if the issue is the same but i just cannot understand what i'm doing wrong here. Please, have patience with me as i am a beginner in C.