Initialization of C-language character arrays
1. Definition and initialization of character array
(1), the first way:
Char str[10]={' I ', ' ', ' a ', ' m ', ' ', ' h ', ' A ', ' P ', ' P ', ' Y '};
Assign 10 characters to str[0] to str[9]10 elements
Note: If the number of characters provided in the curly braces is greater than the array length, then the syntax error is handled, and if it is less than the array length, only those elements that precede those characters in the array are automatically designated as null characters (that is, ' + ').
(2), the second way:
Char str[]={"I am Happy"};
Or:
Char str[]= "I am Happy";
Note: The entire assignment of such a character array can only be used when the character array is initialized, cannot be used for assignment of a character array, and the assignment of a character array can only be assigned to its element one by one.
The following assignment method is incorrect
Char str[];
Str= "I am Happy";//error, the assignment of a character array can only be assigned by element one by one
Note: The length of the array str is not 10, but 11, it is important to remember that because the string constant "I am happy" is automatically added by the system to a '.
(3), the above initialization is equivalent to the following initialization
Char str[]={' I ', ' ', ' a ', ' m ', ' ', ' h ', ' A ', ' P ', ' P ', ' Y ', ' n '};
And 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 ' \ s ', and may not even contain ' s ',
It is perfectly legal to write to the following.
Char str[5]={' C ', ' h ', ' I ', ' n ', ' a '};
As can be seen, the character array is initialized with a single character, and the system will not add ' \ s ' later, and the system will add ' \ ' when initialized as a string.
2, the representation of the string
In the C language, you can represent and store strings in two ways:
(1), storing a string with a character array
Char str[]= "I love China";
(2), pointing to a string with a character pointer
char* str= "I love China";
(3), both representations of the string output are used
printf ("%s\n", str);
Note that for the first representation, the character array name is the first address of the character array, which is consistent with the meaning of the pointer in the second, the system first outputs the character data it points to, and automatically makes STR automatically add 1 to the next character, until the end of the string is encountered.
3, character pointer variable and character array
Although both character arrays and character pointer variables can be used to store and operate strings. A character array consists of several elements, one character for each element, and a character pointer variable that holds an address (the first address of a string/character array), and never places a string in a character pointer variable (the first address of a string).
4, character pointer variable and character array assignment method:
A character array can only be assigned to individual elements, and it cannot be assigned a value in the following ways
Char str[14];
Str= "I love China"; Error, not initialization, this is assignment and can only be done one at a.
(But it can be when the character array is initialized, that is, char str[14]= "I love China";)
For a character pointer variable, the following method is used to assign the value:
Char* A;
A= "I love China";//pointer assignment, you can
or char* a= "I love China"; All can
Note: If a character array is defined, it has a definite memory address that cannot be assigned multiple times, and when a character pointer variable is defined, it does not point to a certain character data and can be assigned multiple times.
5, String handler function
(1), String connection
Char *strcat (char *str1,const char);
Char *strcat (char *strdestination,const char *strsource);
Function: The function connects the string str2 to the end of the STR1 and returns the pointer str1
Note: After the first two strings are connected, there is a ' + ', which will be removed after the string 1, and only a new string is retained at the end.
(2), string copy
Char *strcpy (char *str1,const char);
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 pointer strdestination.
Note:
1, "character array 1" must be written in the form of an array name, "String 2" can be a character array name, or it can be a string constant
2. Copy to array 1 along with the ' \ ' after string
3. You cannot assign a string constant or a character array directly to an array of characters (the same as an array of normal variables) using an assignment statement, but only with the strcpy function.
4, you can use the strcpy function to copy the first number of characters in string 2 into the character array 1.
Dark Horse programmer--c Language Basic Grammar (iii)