Definition and initialization of character arrays in C Language

Source: Internet
Author: User
1. Definition and initialization of character Arrays
The easiest way to initialize a character array is to assign characters to each element in the array one by one.
Char STR [10] = {'I', '', 'A', 'M','', 'h', 'A', 'P ', 'P', 'y '};
Assign 10 characters to STR [0] to STR [9] and 10 elements respectively.
If the number of characters provided in curly braces is greater than the length of the array, syntax errors are handled.
Only the elements in the array of these characters, and the remaining elements are automatically set as null characters (that is
'\ 0 ').
2. Character arrays and strings
In C, strings are processed as character arrays. (C ++ is not)
In practical applications, the length of valid strings is concerned rather than the length of character arrays. For example,
Defines an array of 100 characters, and the actual valid characters are only 40.
Length. The C language specifies a "string ending sign", which is represented by the character '\ 0. If there is a word
String, of which 10th characters are '\ 0', the valid character of this string is 9. In other words
When the first character is '\ 0', it indicates that the string ends and the character before it forms a string.
The system automatically adds '\ 0' to the String constant as the Terminator. For example, "C program" has a total of 9 Characters
But the memory occupies 10 bytes. The last byte '\ 0' is automatically added by the system. (Via sizeof ()
Function verifiable)
With the ending sign '\ 0', the length of the character array is not so important.
Checks the position of '\ 0' to determine whether the string ends, rather than determining the string length based on the array length.
Degree. Of course, the actual string length should be estimated when defining character arrays to ensure that the array length is always greater
The actual length of the string. (In the actual string definition, the array length is often not specified, such as char STR [])
Note: '\ n' indicates a character with an ascii code of 0. From the ASCII code table, you can find that the ASCII code is 0.
A character is not a printable character, but an "null operator", that is, it does nothing. Use it
As the string end flag does not produce additional operations or add valid characters, only one for identification
.
After learning about how to process strings in C language, we can add the methods for initializing character arrays.
One way -- you can use string constants to initialize character Arrays:
Char STR [] = {"I am happy"}; curly braces can be omitted, as shown below:
Char STR [] = "I am happy ";
Note: The overall assignment of the preceding character array can only be used when the character array is initialized and cannot be used
Assign a value to the character array. The assign value to the character array can only assign values to its elements one by one. The following assignment method is as follows:
Incorrect
Char STR [];
STR = "I am happy ";
Instead of using a single character as the initial value, a string is used (note:
"" Rather than enclosed by single quotes ") as the initial value. Obviously, this method is more intuitive and convenient. (Note:
The length of the STR array is not 10, but 11. Remember this because the String constant "I am happy"
The system automatically adds '\ 0 ')
Therefore, the above Initialization is equivalent to the following Initialization
Char STR [] = {'I', '', 'A', 'M','', 'h', 'A', 'P', 'P ', 'y', '\ 0 '};
It is 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.
Note: The character array does not require its last character to be '\ 0' or even' \ 0 '.
This write method is completely legal.
Char STR [5] = {'C', 'h', 'I', 'n', 'A '};
++
It can be seen that the length of the array obtained after the character array is initialized using two different methods 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 );
}
Result: 10 11
3. String Representation
In C, you can use two methods to represent and store strings:
(1) store a string with a character array
Char STR [] = "I love China ";
(2) Use a character pointer to point to a string
Char * STR = "I love China ";
For the second expression method, some people think that STR is a string variable, that is, when the string is defined
I love China is assigned directly to this string variable, which is incorrect.
C language processes string constants by character array, and opens a character array in the memory to store
Put the String constant. When the program defines the string pointer variable STR, it only stores the first address of the string (that is
The first address of the character array of the string) is assigned to Str.
Both representation strings are used for output.
Printf ("% s \ n", STR );
% S indicates that a string is output, and STR is the character pointer variable name (for the first representation method
Character array name is the first address of the character array, which is consistent with the pointer in the second type), the system first
Output the character data it points to, and then automatically add 1 to STR to point to the next character ...,
Until the end identifier of the string "\ 0" is encountered ".
4. Discussion on using character pointer variables and character arrays to represent strings
Although character arrays and character pointer variables can be used to store and operate strings
They are different and should not be confused.
4.1. The character array consists of several elements, each of which contains one character.
The address is placed (the first address of the string/character array). It is never placed in a character pointer variable.
(It is the first address of the string)
4.2. Assignment Method:
Assign values to character arrays only to each element. Do not assign values to character arrays using the following method:
Char STR [14];
STR = "I love China ";
STR [14] = "I love China ";)
For character pointer variables, assign values using the following method:
Char *;
A = "I love China ";
Or char * A = "I love China ";
4.3 assign initial values to character pointer variables (initialization ):
Char * A = "I love China ";
It is equivalent:
Char *;
A = "I love China ";
Initialization of character Arrays
Char STR [14] = "I love China ";
Cannot be equivalent:
Char STR [14];
STR = "I love China ";
The value is incorrect)
4.4 If a character array is defined, it has a fixed memory address.
When the needle variable is used, it does not point to a certain character data and can be assigned multiple times.
5. string processing functions
5.1
Char * strcat (char * str1, const char * 2 );
Char * strcat (char * strdestination, const char * strsource );
Function: connects str2 to the end of str1 and returns the str1 pointer.
Note: There is a '\ 0' behind the first two strings to be connected. During connection, remove' \ 0' behind string 1,
Only keep 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: copy characters in the strsource string to the strdestination string, including null values.
Bundle character. The return value is strdestination.
Note: 1. "character array 1" must be written as an array name. "string 2" can be a character array name or
It is a String constant.
2. Copy the string to array 1 together with '\ 0'.
3. A String constant or character array cannot be directly assigned to
Character array (the same as the ordinary variable array), but can only be processed using the strcpy function.
4. You can use the strcpy function to copy the first several characters in string 2 to character array 1.
.
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.