I. Definition of arrays 1. Definition
L declare the array type
L declare the number of elements in the array (How much storage space is required)
2. Format
Element type array name [number of elements];
For example: int ages [3];
3. Easy to use
L simple initialization: int ages [5] = {19, 19, 20, 21, 25 };
Each element has a unique subscript (INDEX), starting from 0.
L access to array elements: A [I]
4. Initialization
L Initialization Method
Int A [3] = {10, 9, 6 };
Int A [3] = {10, 9 };
Int A [] = {11, 7, 6 };
Int A [4] = {[1] = 11, [0] = 7 };
L Common Errors
Int A [];
Int [4];
Int A [B];
A = {10, 11 };
A [4] = {10, 9, 8, 5 };
L note
Initialization can only be performed while defining Arrays
If you want to define an array of colleagues for initialization, the number of array elements must be a constant or not written.
Array is used as function parameter, and the number of elements can be omitted.
The array is used as the function parameter and transmitted as the address of the entire array. Modifying the value of the array element of the function parameter will affect the external real parameter array.
5. Memory Analysis
L size of the array storage space
L storage space division (memory allocation is performed from high address to low address, but the internal elements of an array are carried out from low to high)
L The role of the array name to view the element address
L Array out-of-bounds attention
6. Other Use Cases
L Array and function parameters
Array elements as function parameters
Array as function parameter (sizeof note)
L traverse array elements
Two ways to traverse (while loop and for loop)
Traverse element values and element addresses
Use of character Arrays
2. Two-dimensional array 1. What is a two-dimensional array
Int ages [5] = {10, 11, 90, 89, 70 };
Int ages2 [3] [5] = {
{10, 11, 90, 89, 70 },
{10, 11, 90, 89, 70 },
{10, 11, 90, 89, 70}
};
L what is a two-dimensional array? Int ages [3] [10]; three classes, each with 10 employees
L is equivalent to 3 rows and 10 columns
L is equivalent to three one-dimensional arrays.
L a two-dimensional array is a special one-dimensional array. Its elements are one-dimensional arrays. For example, int A [2] [3] can be considered to be composed of one-dimensional array a [0] and one-dimensional array a [1]. Both one-dimensional arrays contain three int-type elements.
2. Storage
L storage size
L storage structure and sequence
L storage address problems
3. Initialization
L int A [3] [4] = {1, 2, 3, 4, 5, 6 };
L int A [3] [4] = {{},{},{}};
L simple access to array elements
L int A [] [5] = {3, 21, 31,2, 32, 1 };
L note errors:
Int A [3] [4];
A [3] = {};
4. Traverse
L traverse all elements
L traverse the address
L use cases: wuziqi, Tetris
3. String 1. What is a string?
L simple string "itcast"
L a 'I' is a character
L a string is formed when many characters are combined.
2. String Initialization
L char a [] = "123"; and char a [] = {'1', '2', '3'};, you can compare the size.
L "123" is actually composed of '1', '2', '3', and '\ 0'
L "123" Storage Distribution
L string output "% s", '\ 0' will not be output
3. Functions of \ 0
1> string end mark
2> printf ("% s", name2 );
Output characters starting from the name2 address until \ 0 is encountered.
4. common string processing functions
L strlen (Chinese)
The strlen function is declared in the string. h file.
Strlen function: calculates the string length.
1> calculate the number of characters, not the number of words. One Chinese character is counted as three characters
2> the calculated characters do not include \ 0
3> Number of numbers starting from an address until \ 0 is encountered
Iv. String Array 1. Usage
* A one-dimensional character array stores a string, for example, a char name [20] = "mj"
* To store multiple strings, such as the names of all students in a class, a two-dimensional character array is required, char Names [15] [20] can store the names of 15 students (assuming the name cannot exceed 20 characters)
* If you want to store the names of two students, you can use a three-dimensional character array char Names [2] [15] [20].
2. Initialization
Char Names [2] [10] = {'J', 'A', 'y', '\ 0'}, {'J',' I ', 'M', '\ 0 '}};
Char names2 [2] [10] = {"Jay" },{ "Jim "}};
Char names3 [2] [10] = {"Jay", "Jim "};