March 15th, 2003, 02:38 PM
Can c++ strings be used as char buf?
I'm playing around with some sockets and most of the c based commands utilise char buf[BUFSIZE]. I've been using c++ strings and sending &string whenever a char * is required. I'm aware that a buf can be a string or not depending on wether is is null terminated but is it a bad idea to force a c++ string object into a char buf?
March 15th, 2003, 04:09 PM
why dont u just convert it out of the C++ string to a C string?
March 15th, 2003, 07:12 PM
It is a bad idea only if the function alters the char buffer. This is because, the string object may keep the size of the string allocated in a different member of the class (depending on the implementation of course), and if the function alters the char buffer, interesting things might occur later on. If the function accepts a const char* instead of a char *, then at least you have some assurance that the function is not going to alter the contents. BTW string objects have a c_str() member function which returns the pointer to the char array of the object.
Last edited by Scorpions4ever; March 15th, 2003 at 07:15 PM.