The difference between C++ string and char array

Differences:
char *s1: s1 is a pointer to a string;
char s2[]: s2 is an array of characters;
string s3: s3 is an object of the string class.

[Memory model]
For example:
char *s1 = "hello";
char s2[] = "hello"; The
memory model is as follows
       +-----+ +---+---+---+---+ ---+---+
   s1: | *======> | h | e | l | l | o |\0 |
       +-----+ +---+---+- --+---+---+---+
       +---+---+---+---+---+---+
   s2: | h | e | l | l | o |\0 |
       +---+---+---+---+---+---+

Conversion:
1. Convert
string to char *: If you want to convert string directly to const char * type. There are 2 functions for string.

One is .c_str() and the other is .data member function.

例子如下:
    string s1 = "abcde";
    const char *k = s1.c_str();
    const char *t = s1.data();
    printf("k:[%s] t:[%s]\n", k, t);
    system("pause");

As above, all can be output. The content is the same. But it can only be converted to const char*, if you remove the const, the compilation cannot pass. (The memory pointed to by const char * depends on the life cycle of the string object)

Then, if you want to convert to char*, you can use a member function copy of string to achieve. (Because the memory is maintained independently by char *, newly applied for. It is equivalent to two copies)

    string s1 = "abcde";
    char *data;
    int len = s1.length();
    data = (char *)malloc((len + 1)*sizeof(char));
    s1.copy(data, len, 0);
    data[len] = '\0';
    printf("data:%s\n", data);
    printf("s1:%s\n", s1.c_str());

2.Char * is converted to string: it
can be assigned directly (memory copy assignment)
string s;
char *p = "adghrtyh";
s = p;
Note:
When we define a string type, use printf("%s" ,s); The output will be problematic. This is because "%s" requires the first address of the following object. But string is not such a type. So it must be wrong. There is no problem with cout output, if you must printf output. Then it can be like this: printf("%s",s.c_str()).

3.char[] converted to string:
same as char * converted to sting

4、string转换成char[]:
    char buf[10];
    char nbuf[10];
    string str("abcdeg");
    strcpy(buf, str.c_str());
    strncpy(nbuf, str.c_str(), str.length());
    buf[str.length()] = '\0';
    nbuf[str.length()] = '\0';
    printf("buff:[%s] nbuff:[%s].\n", buf, nbuf);

5. QString is converted to const char *:
(const char *)(QString).toLocal8Bit();

Summary:
Through the above conversion, it can be found that the core is still to firmly grasp the difference between the three. We should start with the differences between the three and experience the transition between them.

Reference article:

1. The difference and conversion between char* char[] and string

2. Conversion between string and char* char[]

3. The problem of mutual conversion between char and string in C++

Recommended reading:

char array to string conversion
--------------------- 
Author: fzzjoy 
Source: CSDN 
Original: https: //blog.csdn.net/u010275850/article/details/ 77512695

Guess you like

Origin blog.csdn.net/xiaoshunzi111/article/details/90551662