strings to arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: strings to arrays

Hybrid View

  1. #1
    kingNewbie Guest

    strings to arrays


    How do you convert a string to an array of chars? Do you have to cast it,
    or is there a function that will do it?

  2. #2
    Danny Kalev Guest

    Re: strings to arrays

    what do you mean? a std::string can be converted to char * by calling
    the c_str() member function:

    std::string s="hello";
    const char * p = s.c_str();

    cout<< p[0]; // displays 'h'

    Danny

    kingNewbie wrote:
    >
    > How do you convert a string to an array of chars? Do you have to cast it,
    > or is there a function that will do it?


  3. #3
    Mike Guest

    Re: strings to arrays


    if you are using a character array to construct your string then by definition
    your string is already an array.
    int i;
    char str[6] ="Hello";//always leave room for the null terminator
    printf("%s\n",str);
    for( i =0;i<6;i++)
    printf("%c",str[i]);

    Mike
    "kingNewbie" <bkjunstNOSPAM@ups.edu> wrote:
    >
    >How do you convert a string to an array of chars? Do you have to cast it,
    >or is there a function that will do it?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center