[C language] Summary of array knowledge (1)

1. Array definition

An array is a collection of ordered data of the same data type. The data types can be int type, char type, float type, pointer, structure, union and other types. Ordered data means that each data in the array has a corresponding subscript, starting from 0 and increasing sequentially. An array with one subscript is a one-dimensional array, an array with two subscripts is a two-dimensional array, and an array with multiple subscripts is a multi-dimensional array.

2. One-dimensional array

Type name array name [constant expression];
such as: int a[6];
6 means there are 6 elements, which is the length of the array

  • The value range of the subscript is [0, array length-1]. Note that the subscript cannot cross the boundary.

  • Generally speaking, constant expressions can be integer constants or symbolic constants and cannot contain variables.

    错误: int i=10;			正确:#define M 15
    	  int a[i];			      int a[M];//符号常量
    
  • Array elements can be assigned and referenced

  • initialization:

    类型名 数组名[数组长度]={初值表};
    int [5]={1,2,3,4,5};
    int [5]={1,2,3};//只对数组前三个元素赋初值 其他两个都为零
    int []={1,2,3,4};//系统会认为只有4个元素
    

3. Two-dimensional array

Type name array name [constant expression 1] [constant expression 2]
Constant expression 1 represents the length of the first dimension of the array (number of rows).
Constant expression 2 represents the length of the first dimension of the array (number of columns).
For example: int a [3][4];
represents three rows and four columns, a total of 12 elements

  • initialization

    类型名 数组名[行数][列数]={初值表};
    int a[3][4]={
         
         {1,2,3,4},{5,6,7,8},{9,10,11,12}};
    int a[3][4]={
         
         {3},{2},{5}};//只对每一行第一列赋初值
    
  • When defining an array, the number of rows can be omitted, but the number of columns cannot be omitted. The following are equivalent

    int a[2][3]={3,5,7,9,11,13};
    int a[ ][3]={3,5,7,9,11,13};
    int a[ ][3]={
         
         {3,5,7},{9,11,13}};
    

4. Character array

Character arrays are also divided into one-dimensional arrays and two-dimensional arrays.

1. One-dimensional character array

char array name [constant expression];
such as: char str[5];

  • initialization
char 数组名[常量表达式]={初值表};
char str[6]={'C','h','i','n','a','\0'};
  • If the number of initial values ​​in the initial value table is less than the length of the character array, the extra elements are "\0" and the initial value is 0. Because the character "\0" represents the integer 0, which is the character with ASCII code 0. But "\0" is not the character '0', and the ASCII code value of the character '0' is 48.

  • When the character array is a continuous string, the length of the array should be the number of array elements + 1, because "\0" must be added

    char str[6]={'C','h','i','n','a','\0'};
    	 等价于char str[6]={"China"};
    

2. Two-dimensional character array

char array name [constant expression 1] [constant expression 2];
such as: char str[5][10];

  • initialization

    char book[4][10]={"hello","the","world","mengyuan"};
    

Guess you like

Origin blog.csdn.net/m0_74102736/article/details/130184131