473,387 Members | 1,464 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,387 software developers and data experts.

char array to string?

Hi, I created an array of characters and want to convert that array to a
string.
I want to do something like this :

char array1 [25]; //with letters already in it
string string1;
string1 = array1;

That wont work will it? what would be the proper syntax? thanks in ahead!

Jul 19 '05 #1
4 255141
pentiumPunk wrote:
Hi, I created an array of characters and want to convert that array to a
string.
I want to do something like this :

char array1 [25]; //with letters already in it
string string1;


Change to "string string1(array);" assuming that your char array is null
terminated.

NR

Jul 19 '05 #2

"pentiumPunk" <Br***@triad.rr.com> wrote in message
news:Tl*********************@twister.southeast.rr. com...
Hi, I created an array of characters and want to convert that array to a
string.
I want to do something like this :

char array1 [25]; //with letters already in it
string string1;
string1 = array1;

That wont work will it? what would be the proper syntax? thanks in ahead!


If the string in array1 is null-terminated:
string1.assign( array1 );
or, better, direct initialization:
string string1( array1 );

If array1 is not null-terminated:
string1.assign( array1, array1+25 );
or: string1.assign( array1, 25 );
And there is a pair of matching constructors that
accept the same parameters:
string string1( array1, array1+25 );
or: string string1( array1, 25 );

Too much choice indeed...

hth
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #3

"pentiumPunk" <Br***@triad.rr.com> wrote in message
news:Tl*********************@twister.southeast.rr. com...
Hi, I created an array of characters and want to convert that array to a
string.
I want to do something like this :

char array1 [25]; //with letters already in it
string string1;
string1 = array1;

That wont work will it?
Why not? C++ is easier than you think.
what would be the proper syntax? thanks in ahead!


Very slightly better is to construct the string instead of assigning to it.

char array1 [25]; //with letters already in it
string string1(array1);

I'm assuming that array1 is null terminated. If not you should write

char array1 [25]; // with 25 letters already in it
string string1(array1, 25);

john
Jul 19 '05 #4
"pentiumPunk" <Br***@triad.rr.com> wrote in message news:<Tl*********************@twister.southeast.rr .com>...
char array1 [25]; //with letters already in it
string string1;
string1 = array1;

That wont work will it? what would be the proper syntax? thanks in ahead!


Yes, that will work... Didn't you try it before posting here?

- Shane
Jul 19 '05 #5

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

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
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?
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
2
by: claire | last post by:
I have a char that i convert to a string as follows m_tHeader.m_sshortname is defined as char; string result = new string(m_tHeader.m_sshortname); The problem is that any '\0' chars in the...
7
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.