Is 1-dimensional array of elements of type char as letters, numbers, and any special characters such as +, -, *, /, $, # ... Write in quotes, eg "I like C ++ "-According to the convention, a string willbe terminated by the null character ('\ 0': letters turong). -Xau Is a cursor (pointer) points to the first character of the string (like the array) Example: string s = "Infoworld"; stored as follows:
In this pointer points to sign tudau s first 'I' Ends with nul lnhu so very different from other languages. For example, in Pascal, each string consists of an array of characters and length byte string length. This structure helps ease Pascal string length returned upon request. Meanwhile, Pascal simply returns the byte length, while C to count until it encounters the character '\ 0'. This is why C Pascal slower in some certain situations 1.Gan value for the string (string assignment) An array of characters: char color [] = "blue"; Char * pointer char * colorPtr = "blue"; colorPtr Pointer pointing to a string literal b in "blue" snorg tees ("blue" otrong constant table) Initialization string as array: snorg tees char color [] = {'b', 'l', 'u', 'e' , '\ 0'}; snorg tees Need to remind you that many constants assigned as the use of quotation marks (") is only valid when initializing the array, ie at the array declaration. The expressions in the following program is not valid: snorg tees mystring = "Hello"; mystring [] = "Hello"; mystring = {'H', 'e', 'l', snorg tees 'l', 'o', snorg tees '\ 0'}; we just can "assign" more v loud constant for an array initialization it. The reason snorg tees is motthao work assignment (=) can not be clarified that an array can only get one element of it. At the time of initializing an array special case, because it's not really an assignation, although it uses sign (=). However, C ++ allows us to assign two static arrays of the same size as follows: chara [] = "Hello", b [6]; // null character hello and a total of 6 characters // declared as static on the two arrays of the same size b = a; Assignments equivalent program fragment: int i = 0; while (a [i] <= 6) b [i] = a [i ++]; Set n the first character of the string s with the symbol that c is 1 in 2 following function: voidstrnset (chars [], charc, intn): void memset (char * Des, INTC, size_tn); 2. What two string constants Try the following program fragment: char * s = "hello"; cout << snorg tees s; and: chars [100]; snorg tees strcpy (s, "hello"); cout << s; Two code even give the same results, but their behavior is completely different. In paragraph 2, you can not write s = "hello" ;. To understand the difference, you need to know the operation of the constant table (string constant table) in C. When the program is executed, the compiler creates an object file containing machine code and a table containing all the string constants declared in the program. In paragraph 1, the statement s = "hello"; s only to determine that the address of the string hello in the constant snorg tees table. Because this sequence is located in the constant table, and is part of the executable code, so you can not change it. You can only use it according to the type of read-only (read-only) snorg tees .To illustrate, you can add commands strcpy (s, "modify"); assigned to the following command in example 1, the compiler will write to the constant error. In paragraph 2, the string hello also exists in the constant table, so you can copy it to the array of characters named s. Because s is not a pointer, the statement s = "hello"; snorg tees will not work. Will be addressed by the pointer. 3. read sequence Read data for character array: char word [20]; cin >> snorg tees word; -dOc String does not accept spaces. -Xau May exceed the size of the array. cin >> setw (20) >> word; // Read 19 characters (one reserved for '\ 0') Read the string with blanks using one of the following syntax: gets (array); // in stdio.h, is not recommended to use cin .get (array); cin .get (array, size); cin .getline (array, delimiter = '\ n'); // Default is the delimiter character '\ n' bone cin .getline snorg tees dong (array, size, delimiter = '\ n'); -Save Input into the array array to the occurrence of two cases + data size reaches the size -1 + delimiter character snorg tees is entered Note: delimiter = '\ n' th idau = Latham default in C ++ , ie without this parameter, the compiler will understand that to default Example: charsentence [80]; cin.getline (sentence, 80); // Default delimiter solution for the getline function or get one absolutely can combine with operator >> like this: cout << snorg tees "Enter the name, age, occupation" << endl; //cin.ignore (); cin .getline (name) >> >> job age; If a program crashes or ends abnormally when working with strings usually because some characters are still in the buffer zone. The result is a program that seems to end sooner than expected. The function fflush () or cin.ignore () will solve this problem. It will clean up all buffers and what is in the buffer zone outside (in the example above, snorg tees it does not really need
No comments:
Post a Comment