Getting started with iOS development☞C language (string, string array, command line parameters), ios Array
String 1. Concept
Double quotation marks indicate strings.
"A string" |
// C language compiler automatically concatenates two parallel strings into one string "A string" "another a string" |
// \ Is the mark of the line feed connection (one line is not written enough) "A looooooooooong \ String" |
Common ASCII codes:
'A' = 65 'A' = 97 '0' = 48 '\ 0' = 0
Int a [10]; // indicates that 40 bytes of memory is allocated in the stack. The first address of the space is.
Char a [10]; // indicates that 10 bytes of memory is allocated in the stack. The first address of the space is.
How to represent and save strings:
The C language does not have a string type, and stores a string in one piece of memory space. It is agreed that an integer 0 (or the character '\ 0') is used to indicate the end of a string.
When using a string, you only need to record the start position of the string.
Strings in C are represented by character Arrays:
Char a [6] = {'h', 'E', 'l', 'l', 'O', '\ 0 '}; // special character array (string)
Char a [6] = "hello"; // simplified format (this \ 0 is omitted)
-> It is equivalent to char * a = "hello ";
2. Initialization
Char str [] = "hello"; // common Char str [] = {'h', 'E', 'l', 'l', 'O', '\ 0 '}; // Partial initialization. In some initialization, the default value of elements not initialized is 0 (ASCII 0 corresponding to \ 0) Char str [8] = {'h', 'E', 'l', 'l', 'O'}; // hello000 Char str [5] = {'h', 'E', 'l', 'l', 'O'}; // This method is not a string, but a character array, because no \ 0 Char str [] = {'h', 'E', 'l', 'l', 'O'}; // incorrect syntax, I don't know the length, but I don't know whether there is 0 behind it. |
""; // Contains a space ""; // Empty string, nothing |
String assignment:
- Assign a value to a char * string. You can use the "=" sign directly.
- Assign a value to a char [] string. Use the strcpy function.
Character string features:
- One thing to understand is that the string ends with \ 0 and is not a string without \ 0.
- Strings are enclosed in double quotation marks.
- The essence of a string is an array (character array)
Char str [] = "hello ";
Str [1] = 'y ';
Printf ("% s", str); // hyllo
Note: string variables are different from ordinary character arrays.
C language requires that the string must end with \ 0 (as the end symbol of the string), so the number of elements in the string variable is one more '\ 0' (0) than the number of elements in the character array) Char a [5] = {'h', 'E', 'l', 'l', 'O'}; // a normal character array (not a string, because no \ 0) Char a [8] = {'h', 'E', 'l', 'l', 'O '}; // partial initialization (the uninitialized part is 0), ending with 0 (a string) Char a [6] = {'h', 'E', 'l', 'l', 'O', '\ 0 '}; // end with \ 0 (a string) Char a [6] = {'h', 'E', 'l', 'l', 'O', 0}; // end with 0 (a string) Note: '0' = 48; // "abc0d" Double quotation marks contain the character '0', not the value 0. '\ 0' = 0; |
3. Output
The principle of % s, which is retrieved one by one from the input "Address" until the "\ 0" position is met.
How to output a string:
- Use the % s placeholder of printf to output
- Use the puts function for output (line feed, output as is)
Char str [] = "how are you ";
Printf ("% s \ n", str); // how are you
Puts (str); // how are you
4. Input
- Use the scanf function to receive strings
- Use the gets function to receive strings
- Use the fgets function to receive strings (recommended !)
/** Three methods for assigning values to character arrays */ # Include <stdio. h> # Include <string. h> Void mystrcpy (char * str1, const char * str2 ){ // * Str2 assigns a value to * str1 characters one by one // * Str2: When '\ 0' is assigned to * str1, * str1 returns 0, and the loop ends! While (* str1 ++ = * str2 ++ )); } Int main (){ Char str [10] = "abc "; // 1. Use loops to assign values to character Arrays For (int I = 0; I <10; I ++ ){ Str [I] = "ABC" [I]; // equivalent to * ("ABC" + I). "ABC" returns the address of A (that is, the first address) } Printf ("str = % s \ n", str); // str = ABC // 2. Use the standard library function to assign values to character Arrays Strcpy (str, "XYZ "); Printf ("str = % s \ n", str); // str = XYZ // 3. assign values to character Arrays Using udfs Mystrcpy (str, "OKOK "); Printf ("str = % s \ n", str); // str = OKOK Return 0; } |
5. library functions
- Calculate the string length (strlen): (calculate the number of characters in the string, note that \ 0 is not included)
Strlen principle: Extracts strings one by one from the input address. Each time a string is retrieved, the counter is set to + 1 until \ 0 is encountered.
- String concatenation (strcat)
Principle: First traverse the first string until \ 0 is encountered, then extract the characters in the second string, start from \ 0, and add a \ 0 character at the end.
The strcpy function copies the source data to the target, and overwrites the original data of the target. The target volume must be able to store the copied data. If the volume is insufficient, an error is returned.
- String comparison (strcmp)
Principle: retrieve each character in the string for one-to-one comparison. If it is found that it is not equal, the comparison will not continue.
# Include <stdio. h> # Include <string. h> # Include <stdbool. h> /** Strcpy string assignment function */ Void test1 (){ Char str [6] = {0}; // indicates that 6 bytes of memory is allocated in the stack. The first address of the space is str (array name) Strcpy (str, "abc"); // assign a value to the character array, str [10] = "abc "; Strncpy (str, "AABBCC", sizeof (str)-2); // only assign values to the first 4 characters (AABB); str [6] = "AABB "; } /** Strcat Append content to a string */ Void test2 (){ Char str [8] = "abc "; Strcat (str, "def"); // str [8] = "abcdef "; Strncat (str, "kkkkkk", 3); // append only the first three characters; str [8] = "abckkk "; } /** Strcmp compares the size of the string content */ Void test3 (){ Char * str1 = "abcd. c "; Char * str2 = "abcf. m "; Strcmp (str1, str2); // return value:-2 (str1 <str2) Strncmp (str1, str2, 3); // compare the size of the first three characters; Return Value: 0 (str1 = str2) Bool r = str1> str2; // compare the address size (both str1 and str2 are addresses) } /** Memset memory cleanup function (clear )*/ Void test4 (){ Char str [8] = "abcd "; Memset (str, 0, sizeof (str); // clears the memory space (starting position, resetting, space size/length ); Strcpy (str, "ABCD"); // reset the value after clearing it. Printf ("str = % s \ n", str ); } Int main (){ Test4 (); Return 0; } |
// The strcmp function is used to verify the logon password! # Include <stdio. h> # Include <string. h> Int main (){ Char pwd [20] = {0 }; Do { Printf ("enter the password :"); Scanf ("% s", pwd ); } While (strcmp (pwd, "abc123 ")); Printf ("the password is correct. login successful! \ N "); Return 0; } |
The pointer variable itself is in the stack, and the pointer variable can point anywhere. (The pointer and pointer point to two spaces)
Two situations of changing the pointer value:
1) modify the pointer value (that is, change the pointer point );
2) modify the value of the space pointed to by the pointer
String Array 1. Definition
String Array: All elements in an array are strings.
If you want to store a bunch of strings, you can use a string array. To put it bluntly, a string array is a two-dimensional array.
String Array:
First: char str [4] [6] = {"aaa", "bbb", "ccc"}; // a two-dimensional array of the char type
Type 2: char * str [4] = {"aaa", "bbb", "ccc"}; // One-dimensional array of char * type
2. Application
Command Line Parameters
Parameters passed in to the program when the program is executed by the command line (terminal. (For example, ls is a command line and-l is a parameter)
(For example,./a. out aaa bbb ccc is a command line parameter)