One or one-D arrays
Store a group of data of the same type.
1, the definition of the array
Element type array name [number of elements];
1 //It's all right .2 intages[5] = {Ten, One, A, the, About};3 intages[5] = {Ten, One};4 intages[5] = {[3] =Ten, [4] = One};5 intAges[] = {Ten, One, -};6 7 //wrong wording8 intAges[];
2. Initialization of arrays
1 // 2 // The 3 int ages[< can be initialized at the same time as the array is defined Span style= "color: #800080;" >5 ]; 4 ages = {10 , 11 , 12 , 14 };
// correct syntax can only be initialized at the same time as the array is defined int ages[< Span style= "color: #800080;" >5 ] = {10 , 11 , 12 , 14 };
1 // correct wording 2 /* 3 int count = 5; 4 int Ages[count]; 5 ages[0] = ten; 6 ages[1] = one; 7 ages[2] =; 8 */
3, the basic use of arrays
1/basic use of arrays2 voidArrayuse ()3 {4 //Array definition Format: type array name [number of elements];5 intages[5] = { +, in, -, -, -};6 //[approx]7ages[1] = in;8 9 /*Ten Ages[0] = +; One ages[1] = +; A ages[2] =; - ages[3] =; - ages[4] = +; the */ - - /* - traverse: View each element of an array sequentially + */ - for(inti =0; i<5; i++) + { Aprintf"ages[%d]=%d\n", I, ages[i]); at } -}
4. Array in-memory analysis
① size of the array storage space
② Partition of storage space (memory allocation is from high address to low address, but an array of internal elements from low to high)
③ the function of the array name to see the element address
1#include <stdio.h>2 3 /*4 prompts the user to enter 5 students ' scores, calculates the average score and outputs5 */6 7 intMain ()8 {9 Ten One //1. Define an array to store the scores A intscores[5]; - - //2. Prompt to enter scores the //used to store total scores - intsum =0; - for(inti =0; i<5; i++) { - //2.1 Tips for entering a student's score +printf"Please enter the score of%d students: \ n", i +1); - //2.2 Storage of current student scores +scanf"%d", &scores[i]); A //2.3 Cumulative Results atSum + =Scores[i]; - } - - //3. Calculate the average score, and the output -printf"average score is%f\n", sum/5.0); - return 0; in}
5. The relationship between array and function and its application
① Array as a function parameter, you can omit the number of elements
② array as a function parameter, passing is the address of the entire array, modifying the value of the function parameter group element, will affect the outside of the real parameter group
1#include <stdio.h>2 voidChangeintarray[])3 {4 //printf ("array==%p\n", array);5 6array[0] = -;7 }8 9 voidChange2 (intN)Ten { Onen = -; A } - - intMain () the { - intages[6] = {Ten, One,Ten, One,Ten, One}; - - //printf ("ages==%p\n", ages); + - Change (ages); + A //Change2 (ages[0]); at -printf"%d\n", ages[0]); - return 0; -}
Two or two-D arrays
A two-dimensional array is a special one-dimensional array: its elements are one-dimensional arrays
1. Definition and initialization of arrays
1 inta[3][4] = {1,2,3,4,5,6};2 inta[3][4] = {{},{},{}};3 //array elements Simple access4 inta[][5] = {3, +, to,2, +,1};5 6 //Note the error:7 inta[3][4];8a[3] = {};
2. Simple application of two-dimensional array
1 /*2 1 Light Blue3 2 dark blue4 3 Yellow5 4 Red6 -1 No7 */8 9 intcubes[5][5] = {Ten{1, -1, -1, -1, -1}, One{1,1, -1,2, -1}, A{4,1,2,2,2}, -{4,3,3,3,3}, -{4,3,3,3,3} the }; -
Dark Horse programmer--c Language--array