C + + string processing functions
string concatenation function strcat
Its function prototype is
strcat (Char[],const char[]);
STRCAT is the abbreviation for string catenate (string concatenation). The function has a two-character array of arguments, which function by connecting the string in the second character array to the back of the string in the preceding character array. The second character array is specified as const to ensure that the contents of the array are not modified during the function call. The concatenated string is placed in the first character array, and the function value that is obtained after the function call is the address of the first character array. For example:
Char str1[30]=″people′s Republic Of″;
char Str2[]=″china″;
Cout<<strcat (STR1, str2)); Calling the Strcat function
Output:
The condition before and after the connection is shown in the figure.
String copy function strcpy
Its function prototype is:
strcpy (char[], const char[]);
strcpy is an abbreviation of string copy (string copying). Its role is to copy the string in the second character array to the first character array, overwriting the corresponding character in the first character array. For example:
Char str1[10], Str2[]=″china″;
strcpy (str1, str2);
After execution, the 5 characters ″china″ and ′\0′ (a total of 6 characters) in the str2 are copied to the array str1.
A few notes on string copy function strcpy:
When calling the strcpy function, the first argument must be an array name (such as a str1), the second argument can be a character array name, or it can be a string constant.
You can use the strcpy function to copy the first few characters from a string into a character array.
You can only assign a string to a character array by calling the strcpy function, and you cannot assign a string constant or an array of characters directly to an array of characters with an assignment statement.
string comparison function strcmp
Its function prototype is
strcmp (const char[],const char[]);
STRCMP is the abbreviation for string compare (string comparison). The effect is to compare two strings. Since these two character arrays only participate in comparisons and should not change their contents, two parameters are added to the const declaration. The following wording is legal:
strcmp (str1, str2);
strcmp (″china″,″korea″);
strcmp (Str1,″beijing″);
The result of the comparison is brought back by the function value:
If the string 1 = string 2, the function value is 0.
If the string is 1> string 2, the function value is a positive integer.
If the string is 1< string 2, the function value is a negative integer.
The rules for string comparisons are the same as those in other languages, that is, two strings are compared from left to right (by ASCII value size) until different characters appear or when ′\0′ is encountered. If all characters are the same, they are considered equal, and if there are different characters, the comparison result of the first different character is the same.
Note: For two string comparisons, you cannot use the following form:
if (STR1>STR2) cout<<″yes″;
Character array names str1 and str2 represent array addresses, which are written to compare two array addresses, rather than strings in an array. A comparison of two strings should be done using the
if (strcmp (str1, str2) >0) cout<<″yes″;
String length function strlen
The function prototype is:
Strlen is the abbreviation for string length (string lengths). It is a function to test the length of a string. The value of its function is the actual length in the string, excluding ′\0′. Such as:
char Str[10]=″china″;
Cout<<strlen (str);
The output is not 10, not 6, but 5.
These are some of the most commonly used string-processing functions, and there are other functions in addition.
The concept of C + + pointers
To make sense of what a pointer is, you have to figure out how the data is stored in memory and how it is read.
If a variable is defined in the program, the variable is assigned an internal deposit at compile time. The system allocates a certain length of space according to the variable type defined in the program. For example, a C + + compilation system typically allocates 4 bytes for an integer variable, 4 bytes for a single-precision floating-point variable, and 1 bytes for a character variable. Each byte of the memory area has a number, which is the "address".
Make sure that you understand the difference between the two concepts of the address of a memory unit and the contents of the memory unit. In the program is generally through the variable name to the internal deposit of the access operation. In fact, the program has been compiled after the variable name has been converted to the address of the variable, the value of the variables are accessed through the address. This method of accessing variable values by variable address is called direct access, or direct access.
You can also use a different approach called Indirect access (indirection). You can define such a special variable in your program, which is specifically used to store addresses.
The diagram above is a schematic diagram of direct access and indirect access. There are two ways to send a value of 3 to a variable:
Send the number 3 directly to the unit identified by the integer variable i.
Send 3 to the unit that the pointer variable i_pointer refers to (this is the cell identified by the variable i). See figure
The so-called point, is through the address to embody.
Because the desired variable unit can be found through the address, it can be said that the address points to the variable unit. So the address is visualized as a "pointer", and the address of a variable is called a pointer to the variable.
If a variable is specifically used to hold another variable address (that is, a pointer), it is called a pointer variable. The value of the pointer variable (that is, the value stored in the pointer variable) is the address (that is, the pointer).