I declared:
auto_ptr<char> a(new char[20]);
My question is how I can initialize a with a string?
Printable View
I declared:
auto_ptr<char> a(new char[20]);
My question is how I can initialize a with a string?
Hi,
U need to call explicitaly strcpy to copy a string.
u can not initialize it directly.
Vinod
I am sorry I don't understand what you said. Actually, strcpy doesn't take the auto_ptr<char> as a parameter type. So how could you use strcpy to copy a string to a in question?Quote:
Originally posted by vinodp
Hi,
U need to call explicitaly strcpy to copy a string.
u can not initialize it directly.
Vinod
strcpy(a.get(), "Codeguru");
Beware of length of the string & allocated char length.
My suggestion is instead of this use STL::string class.
Vinod
auto_ptr can not be used for arrays. It calls delete,Code:auto_ptr<char> a(new char[20]);
not delete [] on the object it owns.
Use one of the standard containers (std::string as
already suggested or something like :
vector<char> a(20);
then if you need to pass to a function that expects a c-style
string, you use : &a[0]
I don't think strcpy(a.get(), "Codeguru") is going to work.Quote:
Originally posted by vinodp
strcpy(a.get(), "Codeguru");
Beware of length of the string & allocated char length.
My suggestion is instead of this use STL::string class.
Vinod
I guess you are right. One way is like this:Quote:
Originally posted by Philip Nicoletti
auto_ptr can not be used for arrays. It calls delete,Code:auto_ptr<char> a(new char[20]);
not delete [] on the object it owns.
Use one of the standard containers (std::string as
already suggested or something like :
vector<char> a(20);
then if you need to pass to a function that expects a c-style
string, you use : &a[0]
auto_ptr<string> a(new string("Hello\n");