A brief analysis of the conversion _c language between string and char* char[]

Source: Internet
Author: User

1. First of all, it must be understood that string can be viewed as a container with characters as elements. Character composition sequence (string). sometimes traversing in a sequence of characters, the standard string class provides an STL container interface. With some member functions such as Begin (), End (), iterators can be positioned according to them.

Note that unlike char*, a string does not necessarily end with null (' "). The string length can be obtained based on length (), and string can be accessed according to the subscript. Therefore, you cannot assign a string directly to char*.

2, String converted to char *

If you are converting a string directly to the const char * type. A string has 2 functions to use.

One is. C_STR (), and one is the data member function.

Examples are as follows:

string S1 = "Abcdeg";
const char *k = S1.C_STR ();
const char *t = S1.data ();
printf ("%s%s", k,t);
cout<<k<<t<<endl;

As above, all can be output. Content is the same. But it can only be converted to a const char*, if you remove the const compilation cannot pass.

Then, if you want to convert to char*, you can use a member function copy of string to implement it.

string S1 = "ABCDEFG";
Char *data;
int len = S1.length ();
data = (char *) malloc ((len+1) *sizeof (char));
S1.copy (data,len,0);
printf ("%s", data);
cout<<data;

3, char * converted to String

You can assign a value directly.

string S;

Char *p = "ADGHRTYH";

s = p;

But this is going to be a problem.

There is a situation I would like to explain. When we define a string type, use printf ("%s", S1), and the output can be problematic. This is because "%s" requires the first address of the object that follows. However, a string is not such a type. So there must be a mistake.

With cout output is no problem, if you must printf output. Then you can do this:
printf ("%s", S1.c_str ())

4, char[] convert to String

This can also be directly assigned to the value. But there will also be problems above. Need the same treatment.

5. Convert string into char[]

This is because we know the length of a string, which can be obtained according to the lengths () function, and can be accessed directly from the subscript, so you can assign a value using a loop.

Such conversions are not directly assignable.

    string pp = "Dagah";
    Char p[8];
    int i;
    for (I=0;i<pp.length (); i++)
        p[i] = pp[i];
    p[i] = ';
    printf ("%s\n", p);
    cout<<p;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.