How to convert string into character array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    How to convert string into character array

    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.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by Man4ish
    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

    [CODE=CPP]
    const char* str = t.c_str();
    char cc[strlen(str)] = t; [/CODE]
    Hope this helps!
    Regards

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      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
      Last edited by gpraghuram; Apr 2 '08, 08:28 AM. Reason: Typo error

      Comment

      • mmk
        New Member
        • Oct 2006
        • 19

        #4
        Originally posted by gpraghuram
        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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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:

          [code=cpp]
          template<class T>
          class basic_string
          {
          T arr[15]
          T* str;
          //etc...
          };
          [/code]

          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:
          [code=cpp]
          string str("Hello");
          char arr[6];
          str.copy(arr, 5);
          [/code]

          Comment

          Working...