C-style string Some notes

Source: Internet
Author: User
Tags strcmp

tags (space-delimited): C + + programming language Introduction:

The strings in C + + are divided into two types: C-style string String Class Library

For the former, it is an array of char types, but the last one must be ' yes '
(We can truncate the string with ' the ');
We can simply define a sample:

Char test[5] = {' t ', ' e ', ' s ', ' t ', ' n '};

Here the ' i ' mechanism makes me think for a long time, in the following output of the place to elaborate

But as tricky as it is, it takes a single quote to alphanumeric one character, so you can define a string called a string literal (string constant) or a string literal (strings literal):

Char test[5] = "Test"; Reserve a ' i ' position
char test[] = ' test '//Let the compiler go to count, then open the space

Yet there is a situation where we have a surplus of space, such as:

Char test[10] = "Test";
Char test[10] = {' t ', ' e ', ' s ', ' t '};

So that the remaining empty characters will be automatically filled '

BTW I'm here on a whim to do a little experiment:

Char test_1[10] = {' t ', ' e ', ' s ', ' t '};
Char test_2[10] = "Test";
if (test_1 = = test_2) {
    cout << "equal" << Endl;
}

When compiling, g++ told me:

Warning:array comparison always evaluates to False [-wtautological-compare]

When the operation is really false,tell me why ...
My classmate later reminded me that the array name is a pointer to the first address of the array, and it is possible to compare the memory with two different addresses ...
——————————————————————
(another topic)

The reason to check C-style string is to see a blog post that says

Defined

void print (const char*);

Then the call is an exact match:

Print ("a");//exact match, call print (const char*)

After consulting the book, we found that the "test" is a constant string representing the memory address of the string, so it should be a const char* type of variable
So

Char test[] = "Test"

We assign the memory address of "test" to the test
So, when cout,

cout << test[] << Endl;

It's not going to go through, but.

cout << test << Endl;

It's okay.

Also, if "test" indicates an address,

cout << "test" << Endl;

It's also possible.

Description C + + in the output of the constant string, the incoming cout and ' << ' parameter is a constant string of memory address, a bit surprised, cout not loyal to the data to resolve the address AH
———————————————————————————

One more experiment:

Const char* test = "Test";
cout << test << Endl;

The result output is test

Const char* test = "Test";
cout << *test << Endl;

The result is only the first letter T

————————————

char* test = "Test";
cout << test << Endl;

This time the compiler gave me a warning:
Warning:conversion from string literal to ' char * ' is deprecated

Description I'm doing a conversion of a string literal to a pointer of character type, so I ignore the warning to run it.
We find out that the output is still test.

So that the const here is not simply a constant immutable meaning
The const char* is listed separately as a data type, which is called a constant string. Action:

cout Stitching:

cout << "Hello"
"World" << Endl;

Both are possible, and the result is the HelloWorld header file:

The header file is not required when used alone, but if you need to invoke some C-style string functions, we need

# include<cstring>

The old-fashioned ones are:

# include<string.h>

Common strlen,strcmp, strcpy, and so on, these functions actually use the time to go to Baidu, or do some of their own small experiments, understand more clearly
... well, I'm too lazy to summarize.
Simply write down:

Strlen
The principle should be to start counting from the first address, to the length of the last character position string that is not ' yes '

strcmp
Prototype for

int strcmp (const char *S1, const char *S2);

The comparison of string sizes is based on the order of the ASCII code table, which is also the value of the character. strcmp () First S1 the first character value minus S2 First character value, if the difference is 0 then continue to compare the next character, if the difference is not 0 will return the difference value. For example, the string "Ac" and "ba" Comparisons return the Difference (-33) of the character "A" (65) and ' B ' (98).

strcpy
Copy the contents of the string S2 to S1, and the string end symbol is also copy.

Strcat
The strcat () function is used to connect a string, and its prototype is:

Char *strcat (char *dest, const char *SRC);

strcat () copies the parameter src string to the end of the string that the parameter dest refers to; dest the last ending character null is overwritten and a null is added to the tail of the concatenated string.

As for the other functions, or some special cases of these functions, when really used, we also need to check and test;

Output:

I have done some experiments on my own, regardless of the contents of this book:

Experiment One:

Char test01[3] = {' A ', ' B ', ' C '};
Char test[3] = {' t ', ' e ', ' s '};

cout << test << Endl;
cout << * (test+3) <<endl;

The result is tesabc and ' a '

Question: Directly define an array of characters, the compiler will not automatically add "" "? If 1 is a meeting, why do I test01 the ' I ' of test.

(It turns out that there is no ' cout ', but the mechanism will be different)

Experiment Two:

Const char* TEST01 = "abc";
Const char* test = "tes";

cout << test << Endl;
cout << * (test+3) <<endl;

The result is "TES" and Empty

The first is understood as a pure character array, then the second is a string constant that appears to be automatically added ' "and will not be overwritten

Then I discussed it with my classmates, the character array is not a string, because the memory of the character array in the above example is accessed directly by the cout array name, which is an improper operation in itself, so it is inevitable that there will be some problems, but this one is really easy to confuse (character array and string), The time to discriminate is still to clarify the thinking. Input:

Constants must not be defined by input, and when we define variable strings, CIN cannot have spaces when entering a string or character array, and secondly, CIN will not read ' \ n ' at the end of the input, but will automatically add ' yes '

If you want to enter an entire row, you can use the Getline function. Summary:

There are three types that are easy to confuse:
Character array
Variable string (that is, an array of characters at the end of ' yes ')
A constant string (a string enclosed in double quotes)

The first two pointer types are char*
The pointer type of a constant string is const char*

An easily confusing operation, as on a

Char test[] = "Test";

"Test" is a constant string, and test is a variable string, and the implementation mechanism is:
When we construct test, we find "test" in the constant table, and then we copy it to test one by one, and then we add ' yes '

Related Article

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.