So I\'m trying to copy one char array to another (without using thestring classe
ID: 3610159 • Letter: S
Question
So I'm trying to copy one char array to another (without using thestring classes), but I'm running into a problem.When I re-initialize dest, I get a large amount of junk; how wouldI go about deleting the stuff after the relevant data?
Function:
void String::Copy(const char* src,char*& dest)
{
int size = GetLength(src); //GetLength(s) isessentially strlen(s)
delete[] dest;
dest = new char[size];
for(int i = 0; i < size; i++)
dest[i] = src[i];
}
For example, using asdf as src, I get this:
src 0x0014f6dc "asdf" constchar *
dest 0x007554d0"asdfýýýý««««««««" char * &
Will rate lifesaver if your solution solves my problem.
Using MSVS 2008.
Explanation / Answer
//Hope this willhelp you.. void String::Copy(const char* src,char*& dest) { int size = GetLength(src); //GetLength(s) isessentially strlen(s) delete[] dest; dest = new char[size+1]; //It should be null terminated ,so one more for nullchar for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.