2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
chapter Ten String
1 String literals(that is, a string constant), is a string enclosed in a pair of double quotes.Character sequenceNo matter how many characters are inside the double quotes, they represent a string literal.
2 "a" is a string literal, while 'a' is a character constant, the two are different.
3 The string is actually composed of severalValid charactersA sequence of characters ending with the character '0'.
1. Access to stringsCharacter arrayBut a character array storesuncertainis a string that can be’0’String is represented only when .
2 The string end mark '0' also indicates that it occupiesOne Byteof memory, but does not include the stringActual length。
3 The allowed forms of string initialization are:
char str[6]={‘h’,’e’,’l’,’l’,’0’,’0’};
You can also omit the declaration of the array length (because the length is obvious, but note that '0' cannot be omitted):
char str[ ]= {‘h’,’e’,’l’,’l’,’0’,’0’};
Or initialize a character array with a string constant:
char str[ ]={“hello”};
You can also omit the curly braces:
Char str[ ]=”hello”;
4 Always remember to leave enough storage space to mark the end of the string. Therefore, the size of the character array is always larger than the actual number of characters in the string.One more。
5 A two-dimensional array isStore by rowTherefore, the system must be informed of the length of the first line. When the length of the string provided by the initialization list is less than the length of each line, the system automatically allocates space for the subsequent cells.”0”。
1 Character pointerIt is a pointer variable pointing to character type data.
2 The string literal itself represents the constant storage area where it is stored.First addressTherefore, only theFirst address assignmentGive a pointer variable, and you can make the character pointer point to a string.
3 Strings are kept inRead-only constant storageTherefore, you can only modifyThe value of the pointer variable, you cannot use pointer variablesThe storage unit pointed toPerform read and write operations.
You can only modify the value of ptr, but not the storage unit pointed to by ptr. Therefore, *ptr='W'; is illegal.
4 If you save the string inAn arrayThen use the character pointer to point to it. This operation is legal. For example: