Definition:
The array name meets the writing requirements of identifier (number, English letter, underline).
Array names cannot be the same as other variable names. They are unique in the same scope.
The constant expression in brackets [] indicates the number of array elements, and inta[3] indicates that array a has three elements, whose subscript starts from 0. Therefore, the three elements are a[0],a[1],a[2].
When defining an array, [] should be a constant. When using an array, [] can be a constant or a variable.
Give an example:
#include <stdio.h> int main() { int a[10];//It defines an array, named a, with 10 members, each of which is of type int //a[0]... a[9], no a[10] //There is no a variable. A is the name of the array, but not the variable name. It is a constant a[0] = 0; //...... a[9] = 9; int i = 0; for (i = 0; i < 10; i++) { a[i] = i; //Assign value to array } //Iterate through the array and output the value of each member for (i = 0; i < 10; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
Initialization:
When an array is defined and assigned at the same time, it is called initialization. If the global array is not initialized, the compiler initializes it to zero. If the local array is not initialized, its contents are random values.
"> int a [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; / / define an array and initialize all member variables at the same time
+ int a [10] = {1, 2, 3}; / / initializes the first three members, and all the following elements are set to 0
'int a [10] = {0}; / / all members are set to 0
The number of elements is not defined in / [], which must be initialized during definition
'int a [] = {1, 2, 3, 4, 5}; / / an array with 5 members is defined
The array name is an address constant that represents the address of the first element in the array.
Code instance:
#include <stdio.h> int main() { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//Define an array and initialize all member variables at the same time printf("a = %p\n", a); printf("&a[0] = %p\n", &a[0]); int n = sizeof(a); //Size of memory occupied by array, 10 int types, 10 * 4 = 40 int n0 = sizeof(a[0]);//The 0 th element of the array occupies the memory size, the 0 th element is int, 4 int i = 0; for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
Common operation examples:
-
Finding the maximum value:
#include <stdio.h> int main() { int a[] = { 1, -2, 3,- 4, 5, -6, 7, -8, -9, 10 };//Define an array and initialize all member variables at the same time int i = 0; int max = a[0]; for (i = 1; i < sizeof(a) / sizeof(a[0]); i++) { if (a[i] > max) { max = a[i]; } } printf("The maximum value in the array is:%d\n", max); return 0; }
- Array inversion:
#include <stdio.h> int main() { int a[] = { 1, -2, 3,- 4, 5, -6, 7, -8, -9, 10 };//Define an array and initialize all member variables at the same time int i = 0; int j = sizeof(a) / sizeof(a[0]) -1; int tmp; while (i < j) { tmp = a[i]; a[i] = a[j]; a[j] = tmp; i++; j--; } for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
- Bubble sorting:
#include <stdio.h> int main() { int a[] = { 1, -2, 3,- 4, 5, -6, 7, -8, -9, 10 };//Define an array and initialize all member variables at the same time int i = 0; int j = 0; int n = sizeof(a) / sizeof(a[0]); int tmp; //1, process //2, trial number for (i = 0; i < n-1; i++) { for (j = 0; j < n - i -1 ; j++)//The purpose of the inner loop is to compare adjacent elements and put the large ones behind { if (a[j] > a[j + 1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } for (i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }