I. Definition of an array
An array is a contiguous variable space of the same type in memory.
Two. How the array is stored in memory
All members of the same array are of the same data type, and all members in-memory addresses are contiguous, and the array name is a constant of an address, representing the address of the first element in the array.
Three. Initialization of arrays
3.1 One-dimensional array initialization
inta[Ten] = {1,2,3 };inta[Ten] = {0 };inta[Ten] = {1,2,3,4,5,6,7,8,9,Ten};intA[] = {1,2,3,4,5,6,7,8,9,Ten};//This is equivalent to the above notation.
Attention:
In the C language, it is extremely dangerous to use an array uninitialized, and the system assigns a random value to each element in the array, so it is best to initialize it before using it.
Exercise 1
Define the following array
int a[10] = {0,1,2,3,4,5,6,7,8,9};
Reverses the elements in the array (the maximum value is before, and the minimum value is behind).
#include <stdio.h>voidMain () {inta[Ten] = {0,1,2,3,4,5,6,7,8,9}; //int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(inti =0; I <Ten; i++) {printf ("a[%d]=%d\n", I,a[i]); } printf ("------------------invert the array------------------\ n"); intMin =0;//record the minimum subscript for the current array intMax =9;//record the maximum subscript for the current array while(Min <max) { intTMP = A[min];//record the current smaller valueA[min] = A[max];//assigns a large subscript value to a lower valueA[max] = tmp;//assigns a smaller subscript value to a larger valuemin++; Max--; } for(inti =0; I <Ten; i++) {printf ("a[%d]=%d\n", I,a[i]); } System ("Pause");}
Operation Result:
C Language Basics (10)-Arrays