1. One-dimensional numerical group

1. One-dimensional numerical group

In programming, for the sake of convenience, arrays organize several variables of the same type in an ordered form. The collection of similar data elements arranged in sequence is called an array. In C language, arrays belong to the constructor data type. An array can be decomposed into multiple array elements, which can be basic data types or constructor types. Therefore, according to the different types of array elements, arrays can be divided into various categories such as numerical arrays, character arrays, pointer arrays, and structural arrays.

This chapter introduces numerical arrays and character arrays, and the rest will be introduced in subsequent chapters. In C language, when using an array, a type specification must be provided first. The general form of array description is: type descriptor array name [constant expression]; Among them, the type descriptor is any basic data type or constructor data type. The array name is a user-defined array identifier. The constant expression in square brackets represents the number of data elements, also known as the length of an array. For example:

int a[10]; // The integer array a has 10 elements。 
float b[10], c[20]; // Real type array b has 10 elements, and real type array c has 20 elements.
char ch[20]; // The character array ch has 20 elements.

The following points should be noted when describing array types:

  1. The type of an array actually refers to the value type of its elements. For the same array, all its elements have the same data type.

  2. The writing rules for array names should comply with the writing regulations for identifiers.

  3. The array name cannot be the same as other variable names, for example:

void main()
{
    int a;
    float a[10];
    //……
  }

是错误的。

  1. 方括号中常量表达式表示数组元素的个数,如a[5]表示数组a有5个元素。但是其下标从0开始计算。因此5个元素分别为a[0],a[1],a[2],a[3],a[4]。

  2. 不能在方括号中用变量来表示元素的个数, 但是可以是符号常数或常量表达式。例如:

2. define FD 5

void main()
{
        int a[3+2],b[7+FD];
        //……
    }

It is legal. However, the following explanation is incorrect.

void main()
{
    int n=5;
        int a[n];
        //……
    }
  1. Allow multiple arrays and variables to be specified in the same type description.

Example: int a,b,c,d,k1[10],k2[20];

2.1. The representation method of array elements

Array elements are the basic units that make up an array. Array elements are also a type of variable, identified by the array name followed by a subscript. The subscript represents the order number of the elements in the array. The general form of array elements is: array name [subscript], where the subscript can only be an integer constant or integer expression. If it is a decimal, C compilation will automatically round it up. For example, a [5], a [i+j], and a [i++] are all valid array elements. Array elements are often referred to as subscript variables. An array must be defined before using index variables. In C language, only index variables can be used one by one, and the entire array cannot be referenced at once.

The general form of initialization assignment is: static type descriptor array name [constant expression]={value, value... value}; Among them, static represents a static storage type, and the C language specifies that only static storage arrays and external storage arrays can be initialized and assigned values (the concepts of static storage and external storage are introduced in Chapter 5). The data values in {} are the initial values of each element, separated by commas. For example:

static int a[10]={ 0,1,2,3,4,5,6,7,8,9 }; 

equal a[0]=0;a[1]=1...a[9]=9;

2.2. Initial assignment of arrays in C language

  1. It is possible to assign initial values to only some elements. When the number of values in {} is less than the number of elements, only the first part of the elements are assigned a value. For example:
static int a[10]={0,1,2,3,4};

Assign values to only 5 elements from a [0] to a [4], with the remaining 5 elements automatically assigned a value of 0.

  1. You can only assign values to each element individually, not to the entire array. For example, assigning a value of 1 to all ten elements can only be written as:
static int a[10]={1,1,1,1,1,1,1,1,1,1};

not as this:

static int a[10]=1;
  1. If no initial value is assigned to an array , all elements will have a value of 0.

  2. If assigning values to all elements, the number of array elements may not be given in the array description. For example:

static int a[5]={1,2,3,4,5};

or:

static int a[]={1,2,3,4,5};

Dynamic assignment can dynamically assign values to logarithmic groups during program execution. At this point, a loop statement can be used in conjunction with the scanf function to assign values to each element of the array one by one.

2.3. Character array

The array used to store the amount of characters is called a character array. The form of character array type description is the same as the numerical array introduced earlier. For example: char c [10]; Due to the universality of character and integer types, it can also be defined as int c [10], but in this case, each array element occupies 2 bytes of memory units. The character array can also be a two-dimensional or multidimensional array, for example: char c [5] [10]; It is a two-dimensional character array. Character arrays also allow for initialization assignment during type specification. For example:

static char c[10]={'c',' ','p','r','o','g','r','a','m'};

The values of each element after assignment are:

c[0] = 'c', c[1] = ' ', c[2] = 'p', c[3] = 'r', c[4] = 'o', c[5] = 'g', c[6] = 'r', c[7] = 'a', c[8] = 'm', c[9] = '\0';

Among them, c [9] is not assigned a value and is automatically assigned a value of 0 by the system. When assigning initial values to all elements, length explanations can also be omitted. For example:

static char c[]={'c',' ','p','r','o','g','r','a','m'};

At this point, the length of the C array is automatically set to 9.

The C language allows initialization assignment of arrays using strings. For example:

static char c[]={'c',' ','p','r','o','g','r','a','m'};

or:

static char c[]={"C program"};

or cancel {} as :

static char c[]="C program";

Assigning values using a string takes up one byte more than assigning values individually using characters, and is used to store the string ending flag '\ 0'. The actual storage of array c in memory is: C program \ 0\ 0 'is automatically added by the C compilation system. Due to the use of the '\ 0' flag, it is generally not necessary to specify the length of the array when assigning initial values to strings, and the system can handle it on its own. After adopting the string format, the input and output of character arrays will become simple and convenient. In addition to the method of assigning initial values to strings mentioned above, the printf and scanf functions can also be used to output strings from an array of characters at once, instead of using loop statements to input and output each character one by one.