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

How to convert string into character array

151 100+
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 17290
Meetee
931 Expert Mod 512MB
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_str() 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 Expert 1GB
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
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 Expert Mod 8TB
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
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
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...
13
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. ...
2
by: Marius Cabas | last post by:
How can I convert a String or a char to byte?
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?
1
by: aurora | last post by:
I something like this: dim mybytes() as byte = "hello world" The string is ascii.
2
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
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...
0
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
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.