By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

How to convert string into character array

100+
P: 151
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
Share this Question
Share on Google+
4 Replies


Meetee
Expert Mod 100+
P: 931
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
Expert 100+
P: 1,275
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

P: 19
mmk
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
Expert Mod 5K+
P: 9,197
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.