473,950 Members | 27,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 69853

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.c omwrote:
>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.c omwrote:
>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<charbuff er(name.begin() , name.end());
buffer.push_bac k('\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
51781
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 have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
5
6208
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
7945
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" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
15
34653
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
12786
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
3
3663
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 = System.Convert.ToString(buffer)
2
2365
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: char = {0, 0, 0, 4, 5, 7}. Now I need to convert it back to a string. Thanks in advance for your help. Steve
8
16274
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 = {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
12
13602
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} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
14
13588
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. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
10171
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10702
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9904
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8268
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6231
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6352
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3557
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.