A detailed description of the use of C-language character arrays and strings

Source: Internet
Author: User

Transferred from:http://www.jb51.net/article/37456.htm

1. Definition and initialization of character array
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, 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. Character array and string
In the C language, strings are treated as character arrays. (not in C + +)
In practice, people are concerned with the length of the valid string rather than the length of the character array, for example, to define a character array length of 100, while the actual valid characters are only 40, in order to determine the actual length of the string, the C language specifies a "string end Flag", which is represented by the character ' + '. If there is a string, where the 10th character is ' + ', then the valid character of this string is 9. That is, when the first character ' + ' is encountered, the end of the string is represented by the character string that precedes it.
The system also automatically adds a ' + ' to the string constant as a terminator. For example, "C program" has a total of 9 characters, but in memory accounted for 10 bytes, the last byte ' \ S ' is the system automatically added. (Can be verified by the sizeof () function)
With the end flag ' \ ', the length of the character array is less important, and in the program it is often dependent on the location of the detection ' s ' to determine if the string is ending, rather than determining the length of the string based on the length of the array. Of course, the actual string length should be estimated when defining a 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 that the ASCII code 0 character is not a can display character, but an "empty operator", that is, it does not do anything. Using it as a string end flag does not produce additional actions or add valid characters, only one token for identification.
The method of handling strings in the C language is understood by the above, and then the method of character array initialization is supplemented by a method-that is, the character array can be initialized with string constants:
Char str[]={"I am Happy"}; The curly braces can be omitted, as shown below
Char str[]= "I am Happy";
Note: The whole assignment of such a character array can only be used when the character array is initialized, cannot be used to assign a character array, the assignment of a character array can only be assigned to its element one by one, the following assignment method is wrong
Char str[];
Str= "I am Happy";

Instead of using a single character as the initial value, use a string (note: The two ends of the string are enclosed in double quotation marks "" instead of single quotes) as the initial value. Obviously, this method is more intuitive and convenient. (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 '
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 ', and it is perfectly legal to write to the following.
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 wrong to think that Str is a string variable that assigns the string constant "I love" directly to the string variable when it is defined.
C Language for string constants is handled by a character array, in memory opened a character array to hold string constants, the program in the definition of the string pointer variable str only the first address of the string (that is, the character array holding the string of the first address) is assigned to Str.
two 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 representation, the character array name is the first address of the character array, and the second one is consistent with the meaning of the pointer), then the system will first output the character data it points to, and then automatically add str automatically 1, so that it points to the next character ... , so until the end of the string is encountered, the identifier "\".

4. Discussion of two methods of using character pointer variable and character array to represent string
Although character arrays and character pointer variables can be used to store and operate strings, they are distinguished from each other and should not be confused.
4.1, the character array is composed of several elements, each element put a character, and the character pointer variable is placed in the address (the first address of the string/character array), never put the string into the character pointer variable (is the string header address)
4.2. How to assign a value:
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 in the character arrayInitializeWhen possible, i.e. 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 an initialization, but an assignment, and an array of values is wrong)
4.4, if a character array is defined, it has a definite memory address, 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 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 ' + ', which will be removed after the string 1, and only a new string is retained at the end.
5.2
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.

A detailed description of the use of C-language character arrays and strings

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.