I have a function that has input and pointer to an array of char in C. in that function I am manipulating the main string, however I want to make a backup copy in another variable before I use it. I want to put it in char backup[2000], so if the pointer changes the backup won't change. How can I do that?
void function (const char *string)
{
char *stringcopy = malloc (1 + strlen (string));
if (stringcopy)
strcpy (stringcopy, string);
else fprintf (stderr, "malloc failure!"):
...
do whatever needs to be done with `stringcopy`
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With