[Study Notes] [C Language] array, Study Notes Array
1. What is an array?
Array, literally, it means a group of data. Yes, arrays are used to store a group of data.
2. array features
Only one type of data can be stored, such as an array of the int type and an array of the float type.
The stored data is called an "element"
3. Definition
Declare the array type
Declare the number of elements in the array (How much storage space is required)
4. Format
Element type array name [number of elements];
For example: int ages [3];
5. Easy to use
Simple initialization: int ages [5] = {19, 19, 20, 21, 25 };
Each element has a unique subscript (INDEX), starting from 0.
Access to array elements: a [I]
6. Initialization
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 };
Common Errors
Int a [];
Int [4];
Int a [B];
A = {10, 11 };
A [4] = {10, 9, 8, 5 };
7. Memory Analysis
Size of the array Bucket
Storage Space Division (memory allocation is performed from high address to low address, but the internal elements of an array are upgraded from low to high)
View the element address.
Array out-of-bounds attention
8. Other Use Cases
Array and function parameters
Array elements as function parameters
Array as function parameter (sizeof note)
Traverse array elements
Two ways to traverse (while loop and for loop)
Traverse element values and element addresses
Use of character Arrays
9. Code
1 # include <stdio. h> 2 3 int main () 4 {5 // Note 6 // all statements are correct 7 // int ages [5] = {10, 11, 12, 67, 56}; 8 // int ages [5] = {10, 11}; 9 // int ages [5] = {[3] = 10, [4] = 11}; 10 // int ages [] = {10, 11, 14}; 11 12 // incorrect syntax 13 // int ages []; 14 15 // incorrect syntax 16/* only 17 int ages [5] can be initialized while defining the array; 18 ages = {10, 11, 12, 14 }; 19 */20 21 // correct syntax 22 // int ages ['a'-50] = {10, 11, 12, 14, 16 }; 23 // int size = sizeof (ages); 24 // printf ("% d \ n", size ); 25 26 // correct syntax 27/* 28 int count = 5; 29 int ages [count]; 30 ages [0] = 10; 31 ages [1] = 11; 32 ages [2] = 18; 33 */34 35 // printf (); 36 // incorrect syntax 37 // if you want to define an array for initialization, the number of array elements must be a constant, or do not write 38 // int ages [count] = {10, 11, 12}; 39 40 41 int ages [] = {10, 11, 12, 78}; 42 43 // calculate the number of array elements 44 int count = sizeof (ages)/sizeof (int); 45 46 for (int I = 0; I <count; I ++) 47 {48 printf ("ages [% d] = % d \ n", I, ages [I]); 49} 50 51 return 0; 52} 53 54 // the basic format of the array is 55 void arrayUse () 56 {57 // The Definition Format of the array: type array name [number of elements]; 58 int ages [5] = {19, 29, 28, 27, 26}; 59 // 19 19 28 27 26] 60 ages [1] = 29; 61 62/* 63 ages [0] = 19; 64 ages [1] = 19; 65 ages [2] = 28; 66 ages [3] = 27; 67 ages [4] = 26; 68 */69 70/* 71 traversal: view every element of the array in sequence 72 */73 for (int I = 0; I <5; I ++) 74 {75 printf ("ages [% d] = % d \ n", I, ages [I]); 76} 77}
Memory storage details of Arrays
1 # include <stdio. h> 2 3/* 4 prompt the user to enter the score of five students, calculate the average score, and output 5 */6 7 int main () 8 {9 10 11 // 1. define an array to store the score 12 int scores [5]; 13 14 // 2. prompt to enter the score 15 // used to store the total score 16 int sum = 0; 17 for (int I = 0; I <5; I ++) {18 // 2.1 prompt: Enter the score of a student 19 printf ("Enter the score of % d students: \ n", I + 1 ); 20 // 2.2 stores the score of the current student 21 scanf ("% d", & scores [I]); 22 // 2.3 accumulates the score 23 sum + = scores [I]; 24} 25 26 // 3. calculate the average score, and output 27 printf ("average score: % f \ n", sum/5.0); 28 return 0; 29} 30 31 32 void test1 () 33 {34 // 1. define an array to store scores 35 int scores [5]; 36 37 // 2. prompt to enter the score 38 printf ("Enter the score of the first 1st students: \ n"); 39 scanf ("% d", & scores [0]); 40 41 printf ("Enter the score of the first 2nd students: \ n"); 42 scanf ("% d", & scores [1]); 43 44 printf ("Enter the score of the first 3rd students: \ n"); 45 scanf ("% d", & scores [2]); 46 47 printf ("Enter the score of the first 4th students: \ n"); 48 scanf ("% d", & scores [3]); 49 50 printf ("Enter the score of 5th students: \ n"); 51 scanf ("% d", & scores [4]); 52 53 // 3. calculate the average score, and output 54 int sum = 0; 55 for (int I = 0; I <5; I ++) {56 sum + = scores [I]; 57} 58 printf ("average score: % f \ n", sum/5.0); 59} 60 61 void test () 62 {63/* 64 char cs [5] = {'A', 'A', 'D', 'E', 'F '}; 65 66 printf ("% p \ n", cs); 67 68 for (int I = 0; I <5; I ++) {69 printf ("cs [% d] address: % p \ n", I, & cs [I]); 70} */71 72 int ages [3] = {10, 19, 18}; 73 74 printf ("% p \ n", ages ); 75 76 for (int I = 0; I <3; I ++) {77 printf ("ages [% d] address: % p \ n", I, & ages [I]); 78} 79}
Arrays and functions
1 # include <stdio. h> 2 3 // array as the function parameter. You can omit the number of elements 4 // array as the function parameter. The transfer is the address of the entire array and the value of the array element of the function parameter is modified, it will affect the fact parameter array 5 6 void change (int array []) 7 {8 // printf ("array = % p \ n", array ); 9 10 array [0] = 100; 11} 12 13 void change2 (int n) 14 {15 n = 100; 16} 17 18 int main () 19 {20 int ages [6] = {10, 11, 10, 11, 10, 11}; 21 22 // printf ("ages = % p \ n ", ages); 23 24 change (ages); 25 26 // change2 (ages [0]); 27 28 printf ("% d \ n", ages [0]); 29 return 0; 30}
Exercise
1/* 2 3 design a function to find the maximum value of integer array elements 4 */5 6 # include <stdio. h> 7 8 int maxOfArray (int array [], int length) 9 {10 // when an array is passed as a function parameter, it is used as a pointer variable, the pointer variable occupies 8 bytes in the 64-bit compiler environment 11 12 // int size = sizeof (array); 13 // printf ("array = % d \ n ", size); 14 15 // sizeof (array); 16 17 // 1. defines the maximum storage value of a variable (the first element by default) 18 int max = array [0]; 19 20 // 2. traverse all elements and find the maximum value 21 for (int I = 1; I <length; I ++) 22 {23 // if the current element is greater than max, overwrite max24 if (array [I]> max) 25 {26 max = array [I]; 27} 28} 29 30 return max with the current element; 31} 32 33 int main () 34 {35 int ages [] = {11, 90, 67,150, 78, 60, 70, 89,100 }; 36 37 int ages2 [] = {11, 90, 67,150, 78, 60, 70, 89,100}; 38 39 // int size = sizeof (ages ); 40 41 // printf ("ages = % d \ n", size); 42 int max = maxOfArray (ages, sizeof (ages)/sizeof (int )); 43 44 printf ("% d \ n", max); 45 return 0; 46}