469,326 Members | 1,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

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 16844
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

Post your reply

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

Similar topics

2 posts views Thread by Venkat | last post: by
14 posts views Thread by Charles L | last post: by
2 posts views Thread by Marius Cabas | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
reply views Thread by =?utf-8?q?C=C3=A9dric_Lucantis?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.