Definition and initialization of C-language character array 1, character array definition and initialization
The easiest way to understand the initialization of a character array is to assigned each element in the array by word.
Char str[10]={' I ', ' ', ' a ', ' m ', ' ', ' h ', ' A ', ' P ', ' P ', ' Y '};
Assign 10 characters to str[0] to str[9]10 elements
If the number of characters provided in the curly braces is greater than the array length, the syntax error is handled;
Length, only those elements that precede those characters in the array are automatically designated as null characters (that is,
' + ').
2. Character array and string
In the C language, strings are treated as character arrays. (not in C + +)
In practice, it is concerned with the length of the valid string rather than the length of the character array, for example,
Defines a character array length of 100, while the actual valid characters are only 40, in order to determine the real
The inter-length, C language Specifies a "string end Flag", which is represented by the character ' s '. If there is a word
String, where the 10th character is ' \ s ', then the valid character for this is 9. In other words, when encountering
The first character, ' + ', represents the end of the string, consisting of the characters in front of it.
The system also automatically adds a ' + ' to the string constant as a terminator. For example, "C program" a total of 9 words
10 bytes in memory, and the last byte ' \ s ' is automatically added to the system. (via sizeof ()
function can be verified)
With the end sign ' \ s ', the length of the character array is less important, and in the program it is often relied on
Detects the position of ' \ ' to determine whether the string ends, rather than depending on the length of the array to determine the string length
Degree. Of course, the actual string length should be estimated when defining the character array, ensuring that the array length is always greater than
The actual length of the string. (in the actual string definition, the array length is often not specified, such as Char str[])
Description: ' \ n ' represents the ASCII code 0 character, from the ASCII code table can be found in ASCII code 0
A character is not a character that can be displayed, but an "empty operator", that is, it does nothing. Use it
As a string end flag does not produce additional actions or add valid characters, only one for the discerning
The logo.
The method of handling strings in C language is understood by the above, and then the method of character array initialization is supplemented
One method-that is, you can initialize a character array with a string constant:
Char str[]={"I am Happy"}; The curly braces can be omitted, as shown below
Char str[]= "I am Happy";
Note: The overall assignment of such a character array can only be used when the character array is initialized and cannot be used for
Assignment of a character array, the assignment of a character array can only be assigned to its element one by one, and the following assignment method is
of the wrong
Char str[];
Str= "I am Happy";
Instead of using a single character as the initial value, use a string (note: Both ends of the string are double-cited
"Instead of the single quotation mark") as the initial value. Obviously, this method is more intuitive and convenient. Note
The length of the array str is not 10, but 11, so be sure to remember that because the string constant "I am happy"
The system automatically adds a ' + ')
Therefore, 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 ', to the following
It is perfectly legal to write this.
Char str[5]={' C ', ' h ', ' I ', ' n ', ' a '};
++++++++
It can be seen that the length of the array is different after initializing the character array in two different ways.
#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 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) point to a string with a character pointer
char* str= "I love China";
For the second representation, it is thought that STR is a string variable that, when defined, is used to
The amount "I love China" is directly assigned to the string variable, which is not correct.
C language for string constants is handled by a character array, in memory to open up a character array to save
string constant, the program defines the string pointer variable str only the first address of the string (that is, storing
The first address of the character array of the string) is assigned to Str.
Both representations of the string output are used
printf ("%s\n", str);
%s indicates the output of a string, given the character pointer variable name STR (for the first notation, the word
Array name is the first address of the character array, which is consistent with the meaning of the pointer in the second type), the system first
Output the character data it points to, and automatically add STR to 1 to point to the next character ...,
This is true until the end of the string identifier "\" is encountered.
4. Discussion of two methods of using character pointer variable and character array to represent string
Although both character arrays and character pointer variables can be used to store and operate strings, they
There is a difference between the two, and should not be confused.
4.1, the character array consists of several elements, each element is placed one character, and the character pointer variable is stored
Place the address (the first address of the string/character array), and never put the string in a character pointer variable
(is the first address of the string)
4.2, the 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"; (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";
or char* a= "I love China"; All can
4.3. Assign the initial value to the character pointer variable (initialize):
char* a= "I love China";
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 the initialization, but the assignment, and the array is assigned
Value is not correct)
4.4, if a character array is defined, then it has a definite memory address, and the definition of a character refers to the
Pin variable, it does not point to a certain character data and can be assigned multiple times.
5. String processing function
5.1
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 ' + ', and the connection will be removed after the string 1.
Only a new string is retained at the end of the
5.2
Char *strcpy (char *str1,const char);
Char *strcpy (char *strdestination,const char *strsource);
Function: Copy characters from string strsource to string strdestination, including null-value knot
The bundle character. 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, you can also
To 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 assignment statement directly to a
An array of characters (the same as an array of normal variables), but only with the strcpy function.
4, you can use the strcpy function to copy the first number of characters in string 2 to a character array 1
to the. From: http://www.cnblogs.com/kungfupanda/archive/2012/06/15/2456931.html
Definition and initialization of C-language character array