Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > convert from string to char array

Reply
Thread Tools

convert from string to char array

 
 
Gary Wessle
Guest
Posts: n/a
 
      11-08-2006
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
 
Reply With Quote
 
 
 
 
doug turnbull
Guest
Posts: n/a
 
      11-08-2006

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.

 
Reply With Quote
 
 
 
 
Peter Jansson
Guest
Posts: n/a
 
      11-08-2006
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


Hi,

c_str() returns a conat char* as you probably know. You should perhaps
(re-)consider why you need a char buffer[128].

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      11-08-2006
Gary Wessle <(E-Mail Removed)> 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


Sure won't, that tries to assign the address of the string object
named "name" to the char array named "buffer". Nonsensical, probably
won't even compile. If it does compile it will probably get you a
memory protection error of some sort.

You need to copy the contents from one place to another:

strcpy(buffer, name.c_str());

--
Tim Slattery
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-09-2006
Tim Slattery wrote:
> Gary Wessle <(E-Mail Removed)> 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

>
> Sure won't, that tries to assign the address of the string object
> named "name" to the char array named "buffer". Nonsensical, probably
> won't even compile. If it does compile it will probably get you a
> memory protection error of some sort.
>
> You need to copy the contents from one place to another:
>
> strcpy(buffer, name.c_str());
>
> --
> Tim Slattery
> (E-Mail Removed)



Better would be to use a vector<char>.

vector<char> buffer(name.begin(), name.end());
buffer.push_back('\0');

You can now access the contiguous data either as a vector, or if you
need to pass it to a legacy API expecting a non-const char*, you can
pass &buffer[0].

 
Reply With Quote
 
=?gb2312?B?seCzzMDL19M=?=
Guest
Posts: n/a
 
      11-09-2006

"Gary Wessle д
"
> 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
>

u can try this :
strncpy(buffer, name , sizeof(buffer)-1);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments