I have 1 function that I want to return the address of an assigned string to the main function and assign an new string pointer with the same address so that the new string will have the contents of the old string.
For example:
unknown_datatype function()
{
char *old = "THE STRING";
return old;
}
int main()
{
char *snew = "";
snew = function();
return 0;
}
*unknown_datatype means I don’t know that to put there…
*How can I approach this without changing anything in the main() method
Typically you will pass the address of the first element in an array of chars in, as well as the length, and have the function fill it.
Edit: You mentioned that for some reason you need to return a char*. I would suggest in that case to use
mallocto allocate the string inside the function, but make sure whenever you call the function youfreethe return value eventually.