473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

convert from string to char array

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
Nov 8 '06 #1
6 69809

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.

Nov 8 '06 #2
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/
Nov 8 '06 #3
Gary Wessle <ph****@yahoo.comwrote:
>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
Sl********@bls.gov
Nov 8 '06 #4
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

Nov 8 '06 #5
Tim Slattery wrote:
Gary Wessle <ph****@yahoo.comwrote:
>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
Sl********@bls.gov

Better would be to use a vector<char>.

vector<charbuffer(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].

Nov 8 '06 #6

"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);

Nov 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
6
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
3
by: David | last post by:
Hi, how to convert a char array(byte) to a string variable? byte buffer; string strTest; /*the blow codes all get type of 'buffer': System.Byte! strTest = buffer.ToString() strTest =...
2
by: Steve Kershaw | last post by:
I can convert from a string to a char array (char) by using string.ToCharArray() function. Now how do I convert it back? The string looks like this: "000457". I've converted it to a char array:...
8
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.