C language character array and the use of strings

  • 2020-04-02 00:53:21
  • OfStack

1. Definition and initialization of character array
The easiest way to understand the initialization of a character array is to assign each element of the array character by character.
Char STR [10] = {' I ', ' ', 'a', 'm', ', 'h', 'a', 'p', 'p', 'y'};
That is, assign 10 characters to 10 elements from STR [0] to STR [9]
If the number of characters provided in curly braces is greater than the length of the array, it is treated as a syntax error. If it is less than the length of the array, only the first elements of the array are set, and the remaining elements are automatically set as empty characters (that is, '\0').

2. Character array and string
In c, strings are treated as arrays of characters. (not in c++)
In practice, people are concerned about the length of the valid string rather than the length of the character array. For example, a character array is defined to be 100 in length when there are only 40 valid characters. To determine the actual length of the string, C specifies a "string end flag", represented by the character '\0'. If you have a string in which the 10th character is '\0', the string has 9 valid characters. That is, when the first character '\0' is encountered, the string ends and is composed of the characters preceding it.
String constants are also automatically terminated with a '\0'. For example, "C Program "has 9 characters but 10 bytes in memory, and the last byte '\0' is automatically added by the system. (verifiable by sizeof() function)
With the end mark '\0', the length of the character array becomes less important, and programs often rely on detecting the position of '\0' to determine whether the string is finished, rather than the length of the array to determine the length of the string. Of course, when defining a character array, you should estimate the actual string length to ensure that the array length is always greater than the actual string length. (in the actual string definition, the array length is often not specified, such as char STR [])
instructions : '\n' represents a character with ASCII code 0. A character with ASCII code 0 is not a character to display, but an "empty operator" that does nothing. Using it as the end of a string does not result in additional operations or additional valid characters, only one flag to identify.
After the above understanding of the C language string processing method, and then to the character array initialization method to add a method -- that is, you can use the string constant to initialize the character array:
Char STR []={"I am happy"};             You can omit the curly braces, as shown below
Char STR []="I am happy";
Pay attention to : the overall assignment of the above character array can only be used when the character array is initialized. It cannot be used for the assignment of the character array
                        Char STR [];
                      STR = "I am happy";

Instead of starting with a single character, start with a string (note: both ends of the string are enclosed in double quotes "", not single quotes" ' '). Obviously, this method is more intuitive and convenient. ( Pay attention to : the length of the array STR is not 10, but 11. Remember this because the string constant "I am happy" is automatically followed by a '\0'.)
Therefore, the initialization above is equivalent to the initialization below
Char STR [] = {' I ', ', 'a', 'm', ', 'h', 'a', 'p', 'p', 'y', '\ 0'};
It's not equivalent to the following
Char STR [] = {' I ', ', 'a', 'm', ', 'h', 'a', 'p', 'p', 'y'};
The length of the former is 11, and the length of the latter is 10.
Description: The character array does not require its last character to be '\0', or even to contain '\0', which is perfectly legal to write as follows.
Char STR [5] = {' C ', 'h', 'I', 'n', 'a'};
++++++++
As you can see, the length of the array after initializing the array of characters in two different ways is different.

# include < stdio.h >
Void main (void)
{
Char c1 [] = {' I ', ', 'a', 'm', ', 'h', 'a', 'p', 'p', 'y'};
Char c2 [] = "I am happy";
Int i1 = sizeof (c1);
Int i2 = sizeof (c2);
Printf (" % d \ n ", i1);
Printf (" % d \ n ", i2);
}
Results: 10     11

3. The representation of strings
In C, strings can be represented and stored in two ways:
(1) store a string in a character array
                    Char STR []="I love China";
(2) use the character pointer to point to a string
                    Char * STR = "I love China";
For the second representation, it is not correct to assume that STR is a string variable and to assign the string constant "I love China" directly to the string variable.
C language for string constant is processed as a character array, in memory to open up a character array to store string constant, the program in the definition of the string pointer variable STR only the first address of the string (that is, the first address of the character array to store string) to STR.
The string output of both representations is used
Printf (" % s \ n ", STR);
% s said output a string of character pointer variable name STR (for the first representation, character array name is the first address of an array of characters, and the pointer in the second meaning is the same), then the system output it is pointing to a first character data, then automatically STR automatic add 1, point to the next character... , until the end of the string identifier "\0" is encountered.

4. Discussion of two methods of representing strings: character pointer variable and character array
While both character arrays and character pointer variables can be used to store and operate strings, there are differences between them and they should not be confused.
4.1. The character array is composed of several elements, with one character for each element; The character pointer variable stores the address (the first address of the string/character array), not the string in the character pointer variable (the first address of the string)
4.2, Assignment method:
        Character arrays can only be assigned to individual elements and cannot be assigned to character arrays in the following way
        Char STR [14].
        STR = "I love China";         But in the character array Initialize the Char STR [14]="I love China";)
        For the character pointer variable, the following method is used:
        Char * a;
        A = "I love China";
        Char * a="I love China";             Can be
4.3 assign an initial value to the character pointer variable ( Initialize the ) :
                Char * a = "I love China";          
Is equivalent to:
                    Char * a;
                    A = "I love China";
And for the initialization of the character array
                  Char STR [14] = "I love China";
Cannot be equivalent to:
                Char STR [14].
                STR = "I love China"; (this is not initialization, it's assignment, and that's not true for arrays.)
4.4 if an array of characters is defined, it has a certain memory address; When a character pointer variable is defined, it does not point to certain character data and can be assigned multiple times.

5. String handling function
5.1
Char *strcat(char *str1,const char *2);
Char *strcat(char *strDestination,const char *strSource);
Function: the str2 function connects the string str2 to the end of str1 and returns the pointer str1
Note: there is a '\0' after the first two strings. When concatenate, remove the '\0' after the first string and keep only one '\0' at the end of the new string.
5.2
Char *strcpy(char *str1,const char *2);
Char *strcpy(char *strDestination,const char *strSource);
Function: copies the characters in the string strSource to the string strDestination, including the null value terminator. The return value is a pointer to strDestination.
Note: 1, "character array 1" must be written as an array name, "string 2" can be a character array name, can also be a string constant
2. Copy to array 1 with '\0' after the string
3. You cannot directly assign a string constant or an array of characters to a string array with an assignment statement (the same as an array of ordinary variables), but only with the strcpy function.
4. You can use the strcpy function to copy the first few characters of the string 2 into the character array 1.


Related articles: