trying to copy one char pointer to another but getting segmentation fault, cant really see whats going on. I also tried doing q = my_strcpy(q, p) but doesnt work. Any help is appreciated.
#include <stdio.h>
char *my_strcpy(char *destination, const char *source);
int main()
{
char *p = "Avani";
char *q = "";
my_strcpy(q, p);
printf("%s", q);
return 0;
}
char *my_strcpy(char *destination, const char *source)
{
char temp = *source;
while (temp != '\0')
{
*destination = temp;
// Increment the pointers, so that they point to the next indexes
source++;
destination++;
// Reassign value of temp to the next value
temp = *source;
}
// Copy the null terminator
*destination = '\0';
// Return pointer to the destination
return destination;
}