how can I pass a pointer by reference? I want this function to duplicate the string. I know only a copy of the pointer is passed, but how do I pass the pointer by reference? I tried having the method accept char*&, but it doesnt enter the method at all (made sure with breakpoints) [beforehand it was just char*].
Thanks in advanced
Code:void strdup (char*& str) { int len=strlen(str); char* strDup = new char[2*len+1]; for (int i=0;i<len;i++) { strDup[i]=str[i]; strDup[i+len]=str[i]; } str= strDup; }
the whole code:
Code:#include <iostream> using namespace std; namespace myStr { int strlen (char* str) { int counter=0; char* ch = &str[0]; while (*ch!=0) { ch++; counter++; } return counter; } void strdup (char*& str) { int len=strlen(str); char* strDup = new char[2*len+1]; for (int i=0;i<len;i++) { strDup[i]=str[i]; strDup[i+len]=str[i]; } str= strDup; } } using namespace myStr; int main () { char str1[] = "Hello"; cout << str1 << endl; cout << strlen(str1) << endl; strdup(str1); cout << str1 << endl; return 0; }



LinkBack URL
About LinkBacks


