473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert string into character array

151 New Member
HI ,
I am trying to convert string into char array of characters.but facing problem.

#include <iostream>
#include <string>
using namespace std;
int main()
{

string t="1,2,3,4,5,6" ;
char cc[strlen(t)]=t;
cout<<cc[1]<<endl;

return 0;
}

df.cpp:8: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

df.cpp:8: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

Please Help me out of this pblm .I will be thankful to you.
Apr 2 '08 #1
4 17329
Meetee
931 Recognized Expert Moderator Contributor
HI ,
I am trying to convert string into char array of characters.but facing problem.

#include <iostream>
#include <string>
using namespace std;
int main()
{

string t="1,2,3,4,5,6" ;
char cc[strlen(t)]=t;
cout<<cc[1]<<endl;

return 0;
}

df.cpp:8: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

df.cpp:8: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

Please Help me out of this pblm .I will be thankful to you.
The problem is you need to pass const char * in strlen function. So here you can use std::string.c_s tr() function like

Expand|Select|Wrap|Line Numbers
  1. const char* str = t.c_str();
  2. char cc[strlen(str)] = t; 
Hope this helps!
Regards
Apr 2 '08 #2
gpraghuram
1,275 Recognized Expert Top Contributor
As told by zodilla it will avoid the compiler error but the array cc wont be initialized properly as strlen cant be used to find the size of the array as array size should be available during compile time and strlen executed at run time


Raghuram
Apr 2 '08 #3
mmk
19 New Member
As told by zodilla it will avoid the compiler error but the array cc wont be initialized properly as strlen cant be used to find the size of the array as array size should be available during compile time and strlen executed at run time


Raghuram
One doutbt I have, actually string means we say its an array of characters or character array. Is this question mean copying a string to an character array ? Can't we use strcpy for this ?

Mmk
Apr 2 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
A string in C++ does not necessarily mean an array of characters as it does in C. It depends upon how the STL version you are using was implemented. The C++ string may be an array of char but it may not. One STL does this:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class basic_string
  3. {
  4.    T arr[15]
  5.    T* str;
  6.    //etc...
  7. };
  8.  
Here the implementor decided that most strings are 15 characters or less, and if so, the array is used to avoid run-time memory allocation. If the string is longer than 15, then a heap allocation is made (takes time) and that is used instead of the array. This was done since STL is optimized for speed and avoiding a heap allocation at run time is faster.

You cannot use strcpy() to make a copy of this thing. strcpy() is part of the C language string library all of which is deprecated in C++. That is, it's in C++ so you can compile antique C code but you should not be writing new code using these C string library functions.

To get a C-string copy from a C++ string, you use the C++ string copy() method:
Expand|Select|Wrap|Line Numbers
  1. string str("Hello");
  2. char arr[6];
  3. str.copy(arr, 5);
  4.  
Apr 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
10668
by: Venkat | last post by:
Hi All, Can someone tell me how do we convert a character array to string. I know the other way. For eg:- String strvar = "hello"; printf("%s",strvar.c_str()); regards,
14
12902
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
13
6097
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. 1. 144 = 0x90 2. 147 = 0x93 3. STX = (^B = 2 = 0x2) 4. NUL = (^@ = 0 = 0x0)
2
38642
by: Marius Cabas | last post by:
How can I convert a String or a char to byte?
15
34568
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?
1
1535
by: aurora | last post by:
I something like this: dim mybytes() as byte = "hello world" The string is ascii.
2
2141
by: quietforever | last post by:
How do you convert a character array into a integer array? For example: If the character array is entered as'12' then how can get an integer array that contains the integers 1 and 2?
2
1896
by: Anarchist | last post by:
I am working on Visual C++ 2005. I have a character array (array<Char>) and want to convert it to string. I know I can do this with the String constructon (gcnew String(charArray)), but is there no other way without allocating memory? I have in mind the GetString method of System::Text::Encoding::ASCII but couldnt manage to do what I wanted to do....
0
1313
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Thursday 19 June 2008 17:12:08 Jonno, vous avez écrit : string = '0.906366 2.276152 0.013369 80.773141' array = -- Cédric Lucantis
0
7520
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...
0
7446
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...
0
7718
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6041
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...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.