C language character array and the use of strings _c language

Source: Internet
Author: User
Tags array length

1, the definition and initialization of character array
The easiest way to understand the initialization of a character array is to assignments each element of the array in one word.
Char str[10]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};
That is, assign 10 characters to str[0] to str[9]10 elements, respectively.
If the number of characters provided in the curly braces is greater than the length of the array, the syntax is handled incorrectly, and if less than the length of the array, only the preceding elements in the array of characters and the remaining elements are automatically set to the null character (that is, ' the ').

2, character array and string
In C, a string is treated as an array of characters。 (not in C + +)
In practical applications, peopleis 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 provides a "string end flag", represented by the character ' ". If there is a string where the 10th character is ', ' then the valid character for this string is 9. That is, when the first character ' is encountered ', the end of the string is represented, and the character in front of it is the string.
System onStringConstantAlsoAutomatically add a ' plus 'As the end character. For example"C Program" has a total of 9 characters, but occupies 10 bytes in memory, the last byte ' is ' is automatically added by the system. (Validated by the sizeof () function)
The length of the character array becomes less important after the end sign ' is '., it is often in a program to determine whether a string is closed by detecting the location of ' the ' ", rather than determining the length of the string based on the length of the array. 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 length of the string. (in the actual string definition, array lengths are often not specified, such as Char str[])
Description: ' \ n ' represents a character with an ASCII code of 0, and the ASCII-0 character from the ASCII-code table is not a character that can be displayed, but rather an "empty operator", that is, it does nothing. Using it as a string ending flag will not produce additional operations or add valid characters, only a token for identification.
The method of handling strings in the C language is understood, and the method of initializing the character array is supplemented by a method-that is, you can initialize the character array with string constants:
Char str[]={"I am Happy"};You can omit curly braces, as shown below
Char str[]= "I am Happy";
Attention: The overall assignment of the above character arrayCan only be used in character arrays when initialized, an assignment that cannot be used for a character array , an assignment of a character array can only be assigned to its element one by one, and the following assignment method is an error
              Char str[];
            str= "I am Happy"; Instead of using a single character as an initial value,

uses a string (note: The ends of the string are enclosed in the double quotes "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, so be sure to remember that because the string constant "I am happy" is finally automatically added by the system with a ' \ 0 ')
Therefore, the above initialization is equivalent to the following initialization
char str[]={' I ', ' ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y ', ', '};
instead of the following equivalence
char str[]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};
the length of the former is 11, and the latter is 10. The
Description: An array of characters does not require that its last character be ' a ' or even ' not ', and it is perfectly legal to write to the following.
Char str[5]={' C ', ' h ', ' I ', ' n ', ' a '};
visible, the array length is different when the character array is initialized with two different methods.

#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) withCharacter arrayStoring a string
Char str[]= "I Love";
(2) withCharacter pointerPoint to a string
char* str= "I Love";
For the second representation, it is not correct 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 to string constants are processed by character array, in memory to open up a character array to hold the string constants, the program in the definition of string pointer variable strJust put the string first address(i.e.The first address of the character array that holds the string) is assigned to Str.
the string output of two representations is used
printf ("%s\n", str);
%s represents the output of a string, given the character pointer variable named STR (for the first representation, the character array name, which is the first address of the character array, is the same as the pointer in the second, the system prints one of the character data it points to, and then automatically makes STR automatically add 1 to the next character ... , so until the string end identifier "" is encountered.

4. Discussion on the representation of strings using character pointer variables and character arrays in two ways
Although both 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, one character for each element, and the address (the first address of the string/character array) in the character pointer variable, not the string in the character pointer variable (the first address of the string).
4.2, Assignment Method:
You can assign values to character arrays only to individual elements, and you cannot assign a character array in the following ways
Char str[14];
Str= "I Love"; (But in character arraysInitialization ofCan be, that is char str[14]= "I love";
For character pointer variables, the following method is used to assign values:
Char* A;
A= "I Love";
or the char* a= "I Love"; All can
4.3. Assign an initial value to the character pointer variable (Initialization of):
char* a= "I Love";
Equivalent to:
Char* A;
A= "I Love";
And for the initialization of character arrays
Char str[14]= "I love";
Cannot be equivalent to:
Char str[14];
Str= "I Love"; (This is not initialization, it's assignment, and it's not right to assign this to an array)
4.4. If you define an array of characters, it has a defined memory address, and when you define a character pointer variable, it does not point to a certain character data and can be assigned multiple times.

5, string handling function
Char *strcat (char *str1,const char *2);
Char *strcat (char *strdestination,const char *strsource);
Function: The function connects the string str2 to the end of the str1, and returns a pointer str1
Note: The first two strings are concatenated with a ' ', which is removed after the string 1, and only the new string is finally retained with a '. '
Char *strcpy (char *str1,const char *2);
Char *strcpy (char *strdestination,const char *strsource);
Feature: Copies the character in the string strsource to the string strdestination, including the null-value terminator. The return value is the pointer strdestination.
Note: 1, "character array 1" must be written as an array name, "String 2" can be a character array name, or it can be a string constant
2, copied to the array 1, along with ' the ' and ' after the string '
3, You cannot assign a string constant or an array of characters directly to an array of characters (the same as an array of ordinary variables) directly with an assignment statement, but only with the strcpy function.
4, you can use the strcpy function to copy the first few characters in string 2 to the 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.