C language learning [5]-array *****

1. Array

The C language supports an array data structure, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.

Specific elements in the array can be accessed by index.

All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

1.1 Declaring an array

//一维数组,arraySize必须是大于0的整数常量,是数组元素的个数。type是任意的C数据类型。
type arraName[arraySize];

1.2 Initialize the array
In C, you can initialize one by one, or you can use an initialization statement.
E.g:

int a[5]={
    
    1,2,3,4,5}
//{}内的个数不能大于[]中指定的元素数目。
//若没写[]内的数,则数组大小为初始化时元素的个数。

All arrays use 0 as the index of their first element, which is also called the base index. The last index of the array is the total size of the array minus 1.

1.3 Access array elements

Array elements can be accessed by adding an index to the array name. The index of the element is placed in square brackets, following the array name.

1.4 Multidimensional array

C supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array.

The statement is as follows:

type name[size1][size2]...[sizeN];

Two-dimensional array:

//一个二维数组,在本质上,是一个一维数组的列表
type arrayName [ x ][ y ];

Initialization:
You can initialize by specifying a value for each line in parentheses:

int a[3][4] = {
    
      
 {
    
    0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
 {
    
    4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
 {
    
    8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
};

The inner nested parentheses are optional:

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

The elements in a two-dimensional array are accessed by using subscripts (that is, the row index and column index of the array).

1.5 Passing an array to a function

You can pass a pointer to the array to the function by specifying the name of the array without an index.

If you want to pass a one-dimensional array as a parameter in a function, you must declare the formal parameters of the function in the following three ways. The results of these three declaration methods are the same, because each method tells the compiler to receive an integer pointer . Similarly, you can also pass a multi-dimensional array as a formal parameter.

Method 1: The formal parameter is a pointer

void myFunction(int *param)
{
    
    
.
.
.
}

Method 2: The formal parameter is an array of a defined size

void myFunction(int param[10])
{
    
    
.
.
.
}

Method 3: The formal parameter is an array of undefined size

void myFunction(int param[])
{
    
    
.
.
.
}

1.6 Return an array from a function

C allows returning arrays from functions.

C language does not allow to return a complete array as a function parameter. However, you can return a pointer to the array by specifying the name of the array without an index.

If you want to return a one-dimensional array from a function, you must declare a function that returns a pointer, as follows:

int * myFunction()
{
    
    
.
.
.
}

In addition, C does not support returning the address of a local variable outside a function, unless the local variable is defined as a static variable.

1.7 Pointer to array

You can generate a pointer to the first element in the array by specifying the name of the array without an index.

The array name is a constant pointer to the first element in the array.

Guess you like

Origin blog.csdn.net/qq_46009608/article/details/110407245