jeudi 2 avril 2020

How do I return a string using this string? Isn't creating a heap a wrong method in a function?

charPointer getCopy(const charPointer& s) //typedef char* charpointer
{
/*
returns a new cstring that is a copy of the cstring s.
That is a new cstring with as big memory as the size of
the cstring s is created and then all the characters of
s including the null char are copied to it.
*/
int len=cstrlen(s);
char *A=new char[len];
for(int i=0;i<len;i++)
{
    A[i]=s[i];
}
A[len]='\0';
return A;

} 1. How do I return the same string? I'm new to c++ please help!!

Aucun commentaire:

Enregistrer un commentaire