From: bowman AT montana DOT com (bowman) Newsgroups: comp.os.msdos.djgpp Subject: Re: Converting a string to a char * References: <840i65$auc$1 AT bob DOT news DOT rcn DOT net> User-Agent: slrn/0.9.5.7 (UNIX) Lines: 27 Message-ID: Date: Sat, 25 Dec 1999 17:34:15 GMT NNTP-Posting-Host: 208.4.224.153 X-Trace: newsfeed.slurp.net 946143255 208.4.224.153 (Sat, 25 Dec 1999 11:34:15 CDT) NNTP-Posting-Date: Sat, 25 Dec 1999 11:34:15 CDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Andrew R. Gillett wrote: >I don't know exactly how c_str() works - does the char array created by >it continue to exist after the line on which it is called? Not reliably. To quote Stroustrup, "The user cannot rely on its value after a subsequent call on a non-const function on the string." He is talking about data(), but the next sentence states c_str is like data, but adds the zero terminator. (C++ Programming Language 3rd Ed pg 589) Also, the preferred method to get the string into a buffer is the copy() member of the basic_string class, if you're a C++ purist. Of course, if you're a purist, you probably are using the C++ interface rather than the C version. One possible pitfall is C++ strings may contain embedded '\0' characters, since the length is of the string is stored separately, and does not depend on the '\0' terminator. The entire string will be correctly copied to a buffer, but the C style functions will not see anything past the first '\0'. Also, it is possible to derive classes from basic_string that will not act as expected if converted to C style and handled with the usual calls. C is not always graceful if naive assumptions are made when handling internationalization.