converting between strings and char array

is there a way to convert string variables to char array under GNU? In
watcom compiler , you can simply do

String name_as_string;
char name_as_char [100];
strcpy (name_as_string, name_as_char);

I learned that under GNU, i need to include as opposed to
<String.h> in watcom, and declare
string variables as string vs String in watcom. I realized that under GNU,
any function that takes char *
as varibles can’t take string variables… like system(), this was allowed in
Watcom.

ran zhang wrote:

is there a way to convert string variables to char array under GNU? In
watcom compiler , you can simply do

String name_as_string;
char name_as_char [100];
strcpy (name_as_string, name_as_char);

I learned that under GNU, i need to include as opposed to
String.h> in watcom, and declare
string variables as string vs String in watcom. I realized that under GNU,
any function that takes char *
as varibles can’t take string variables… like system(), this was allowed in
Watcom.

This was “allowed” (or more correctly “provided for” via operator
overloading), since at the time of Watcom C++, the C++ community hadn’t
yet figured out that automatic conversions are not a good thing (they
don’t play well with function overloading for instance). With standard
ANSI C++ you must access the internal array by calling the member
function c_str(). This is/was completely intentional on the part of the
standards committee. You could overload the cast operator yourself to
give you the same functionality as Watcom C++ but I wouldn’t recommend
it (except perhaps to get an initial port over quickly as a
proof-of-concept or something).

ran zhang <rzhang@vamcointernational.com> wrote:

is there a way to convert string variables to char array under GNU? In
watcom compiler , you can simply do

Well, don’t think this is a “GNU” feature.
Under QNX 6.1 you’re using a GNU compiler with an ANSI C++ library
(the dinkum one).
Lots of things have changed in the c++ draft. You should consider
reading the online reference of the dinkum library at dinkum.com
or, better, get a recent c++ book.

However, string.c_str() will return a new allocated char* string.

String name_as_string;
char name_as_char [100];
strcpy (name_as_string, name_as_char);

I learned that under GNU, i need to include as opposed to
String.h> in watcom, and declare
string variables as string vs String in watcom. I realized that under GNU,
any function that takes char *
as varibles can’t take string variables… like system(), this was allowed in
Watcom.


Wave++

In article <9v2sjp$99s$1@inn.qnx.com>,
ran zhang <rzhang@vamcointernational.com> wrote:

is there a way to convert string variables to char array under GNU? In
watcom compiler , you can simply do

String name_as_string;
char name_as_char [100];
strcpy (name_as_string, name_as_char);

I learned that under GNU, i need to include as opposed to
String.h> in watcom, and declare
string variables as string vs String in watcom. I realized that under GNU,
any function that takes char *
as varibles can’t take string variables… like system(), this was allowed in
Watcom.

Note the example using c_str() in:

http://www.comeaucomputing.com/techtalk/#atoi

Also, check out:

http://www.comeaucomputing.com/techtalk/#stringtostring

which I believe discusses your question.

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?