doug turnbull wrote:
>
Gary Wessle wrote:
Hi
I have
char buffer[128] = "jackson";
how can I get the same effect by
string name = "jackson";
char buffer[128] = name; //will not work
I tried
conat_cast<char*(name.c_str());
which fails
thanks
Assignment doesn't work with c-strings. C strings are a different
animal from the nice C++ string class. You need to use strcpy, to copy
data from name.c_str() into your buffer. Look here
(http://www.cplusplus.com/ref/cstring/) for more info on functions to
deal with c strings. Also google "c strings" for more info.
He's not attempting assignment here (which of course doesn't work with
any array type), but rather initialization.
The rules for initializing an array of char include (C99):
[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.
Clearly, the pointer returned by a call to c_str() is not a character
string literal.
Brian