Array articles

One-dimensional array

  1. Description format: type identifier [expression];
  2. Access array: (1) subscript form; (2) pointer form. Examples of the two methods are as follows:

/**********************************************************
* 函数:AccessArray
* 描述:访问数组的方式
* 参数:无
* 返回:无
* 备注:1.下标 2.指针形式
**********************************************************/
void AccessArray(void)
{
    
    
    int i;
    int* p;
    static int a[5] = {
    
     1,2,3,4,5 };//定义一个静态的int类型数组

    //下标访问
    cout << "以下标方式访问数组" << endl;
    for (i = 0; i < 5; i++)
    {
    
    
        cout << "a[" << i << "]=" << a[i] << " ";
    }
    cout << endl;

    //指针形式访问-1
    cout << "以指针方式访问数组-1" << endl;
    for (i = 0; i < 5; i++)
    {
    
    
        cout << "a[" << i << "]=" << *(a+i) << " ";
    }
    cout << endl;

    //指针形式访问-2
    cout << "以指针方式访问数组-2" << endl;
    p = a;
    for (i = 0; i < 5; p++,i++)
    {
    
    
        cout << "a[" << i << "]=" << *p << " ";
    }
    cout << endl;
    //数组元素的地址
    cout << "数组元素的地址:" << endl;
    p = a;
    for (i = 0; i < 5; p++, i++)
    {
    
    
        cout << "a[" << i << "]的地址:" << p << endl;
    }
}

Operation result: It
Array access
should be noted here: 1. The array name a is a constant , a==&a[0], so self-addition a++ cannot be performed, and its value can only be assigned to the pointer variable of the same type *p, p can be added by itself. If there is no such pointer variable, it can only be accessed in the form of *(a+i). 2. It can be seen from the address of each element of the array that the address of each element has a difference of sizeof(int)=4, and the arrangement of the one-dimensional group elements in the memory is sequential, and the difference in the address of each element is equal to the space occupied by its element type the size of. 3. The compiler will not check if the array access exceeds the limit, so please pay attention.

Pointer array

definition

Explanation form: type * identifier [expression]; for example:
int *pi[3];
char *pf[5]; the
difference between a pointer array and an array of basic types such as int type is that the type of array stored is different, and what is stored is Pointer type.
Note: int *pi[3] cannot be written as int (*pi)[3]; pi in int *pi[3] is an array of pointers, but
pi in int (*pi)[3] is a pointer to a one-dimensional array (Length 3) pointer. It can be explained according to the priority of the symbol: in int *pi[3], the [] symbol has a higher priority than the * symbol, so [] is first combined with pi, pi is an array first, and then combined with the left side , Indicating that the data type of the array element is int *; for int (*pi)[3], the priority of the () symbol is higher than the priority of the [] symbol, so * is first combined with pi, pi is a pointer, int and [3] Explain that the type of the array associated with the pointer is a one-dimensional array of 3-length int type.

An array of pointers to basic array types

Basic data types such as int, char and other data types. The above int *pi[3] is an array of pointers to the basic array type. As the name implies, the address (pointer) of the basic data type is stored in the array. This type of array can be used to manage variable addresses.

Array of pointers to array

It can be analogized to an array of pointers to arrays: that is, pointers to other arrays are stored inside, but the types of these arrays must be the same, but the lengths do not have to be the same. In fact, "array of pointers to basic array types" can be regarded as a special case of "array of pointers to arrays". When the length of the array in "array of pointers to arrays" is 1 and =, it can be regarded as "pointing to basic array types" Pointer array". The "array of pointers to basic array types" stores an array address, through which each member of the array can be accessed. Examples are as follows:

/**********************************************************
 - 函数:PointerArray_PointArray
 - 描述:指向数组的指针数组
 - 参数:无
 - 返回:无
 - 备注:
**********************************************************/
void PointerArray_PointArray(void)
{
    
    
    int a[2] = {
    
     1,2 };
    int b[3] = {
    
     3,4,5 };
    int c[4] = {
    
     6,7,8,9 };
    int* pi[3] = {
    
     a,b,c };//数组名就是数组的地址

    int i;

    //访问数组a
    cout << "访问数组a" << endl;
    for (i = 0; i < 2; i++)
    {
    
    
        cout << "a[" << i << "]=" << *(pi[0]+i) << " ";
    }
    cout << endl;
    //访问数组b
    cout << "访问数组b" << endl;
    for (i = 0; i < 3; i++)
    {
    
    
        cout << "b[" << i << "]=" << *(pi[1] + i) << " ";
    }
    cout << endl;
    //访问数组c
    cout << "访问数组c" << endl;
    for (i = 0; i < 4; i++)
    {
    
    
        cout << "c[" << i << "]=" << *(pi[2] + i) << " ";
    }
    cout << endl;
}

operation result:
Array of pointers to array

Array of pointers to functions

It can also be analogized to an array of pointers to functions: that is, a function pointer (function pointer) is stored in the array, which is the entry address of a function, and the function can be called through the function pointer. And the function has function name = function pointer = function address , so the function name can be directly stored in the array, or & function name can also be used, they are all equal.

definition

  • Use typedef to define the function type first, and then define the array. For
    example: typedef int FuncType(int ,int); FuncType *pFunc[3];
    This defines an array of pointers to FuncType type functions.
  • Directly define, follow the above, such as: int (*pFunc[3])(int,int).
  • Initialize the array after the definition.

Example

/**********************************************************
* 函数:PointerArray_PointFunc
* 描述:指向函数的指针数组
* 参数:无
* 返回:无
* 备注:
**********************************************************/
void PointerArray_PointFunc(void)
{
    
    
    int i;
    FuncType* PFunc[3] = {
    
    f1,f2,f3};

    for (i = 0; i < 3; i++)
    {
    
    
        PFunc[i]();//也可以写成(*PFunc[i])();
    }
}

/**********************************************************
* 函数:f1
* 描述:函数1
* 参数:无
* 返回:无
* 备注:调用时打印“调用函数f1”
**********************************************************/
void f1(void)
{
    
    
    cout << "调用函数f1" << endl;
}

/**********************************************************
* 函数:f2
* 描述:函数2
* 参数:无
* 返回:无
* 备注:调用时打印“调用函数f2”
**********************************************************/
void f2(void)
{
    
    
    cout << "调用函数f2" << endl;
}

/**********************************************************
* 函数:f3
* 描述:函数1
* 参数:无
* 返回:无
* 备注:调用时打印“调用函数f3”
**********************************************************/
void f3(void)
{
    
    
    cout << "调用函数f3" << endl;
}
//指向函数的指针数组
typedef void FuncType(void);
FuncType f1, f2, f3;
void PointerArray_PointFunc(void);

Call the PointerArray_PointFunc function in the main function, and the results are as follows:
Array of pointers to functions

Two-dimensional array

Definition and initialization

  • Explanation format: Type array name [Expression 1] [Expression 2];
    Expression 1: The length of the first dimension, which can be regarded as a row in the matrix;
    Expression 2: The length of the second dimension, which can be regarded as a column in the matrix ;
  • Initialization: The description of the highest dimension length can be omitted. Such as:
  1. int a[2][3] = { {1,2,3},{4,5,6}};
  2. int a[2][3] = {1,2,3,4,5,6};
  3. int a[2][3] = { {1,2,3},{4,5}};//Partial element initialization
  4. int a[][3] = { {1,2,3},{4,5,6}};//Omit the description of the highest dimension length

access

  1. There are also two forms of accessing two-dimensional arrays: subscript access and pointer access.
  2. The understanding of subscript access is relatively simple, and before understanding the access of pointers, you should know or understand such a summary: the array structure is recursive, and each element of an n-dimensional array is an n-1 dimensional array . One-dimensional arrays can be understood as a special case of multi-dimensional arrays. To understand pointer access to one-dimensional arrays, it is easier to understand pointer access to two-dimensional arrays.
  3. (1) First, the array name of a one-dimensional array is a constant pointer, that is, it cannot be assigned to it, and it is equal to the address of the first element , such as int a[3], a==&a[0]. By analogy to a two-dimensional array, such as int a[2][3], a==&a[0], although the writing is the same, the meaning is different. For a one-dimensional array, a[0] is an element of type int, and for a two-dimensional array, a[0] represents an array. It can be analogized to an n-dimensional array, a[0] represents an n-1 dimensional array. So a as a pointer, for arrays of different dimensions, the associated types are different: for an n-dimensional array, a is associated with an n-1 dimensional array . In fact, it can be simply understood that each element of the n-dimensional array stores the array name of the n-1 dimensional array, and each element of the n-1 dimensional array stores the array name of the n-2 dimensional array. This recursively continues, and finally pushes to The case of one-dimensional arrays stops.
    (2) In a one-dimensional array, a+i==&a[i] is also valid in a two-dimensional array, but the meaning is different as described in (1). In a one-dimensional array, the address offset of each adjacent element is sizeof (type), for a two-dimensional array, the offset of the adjacent element is (sizeof (type) * one-dimensional array length), which is extended to n Dimensional array, the offset is (sizeof(type)*n-1 the number of elements in the dimensional array).
  4. Examples of pointer access and subscript access are as follows:
/**********************************************************
* 函数:TwodimensionalArray
* 描述:二维数组
* 参数:无
* 返回:无
* 备注:二维数组定义与访问示例
**********************************************************/
void TwodimensionalArray(void)
{
    
    
    int a[2][3] = {
    
     {
    
    1,2,3},{
    
    4,5,6} };
    int i,j;
    int(*p)[3];//定义一个关联类型为长度为3的整形数组的指针

    //各个元素的地址
    cout << "各个元素的地址" << endl;
    for (i = 0; i < 2; i++)
    {
    
    
        for (j = 0; j < 3; j++)
        {
    
    
            cout << "a[" << i << "]" << "[" << j << "]地址:" << &a[i][j]<< endl;
        }
    }
    cout << endl;

    //二维数组中的每个元素的地址
    cout << "二维数组中的每个元素的地址" << endl;
    for (i = 0; i < 2; i++)
    {
    
    
        cout << "第" << i << "个元素地址:" << a + i << endl;
    }
    cout << endl;

    //下标访问二维数组
    cout << "下标访问二维数组" << endl;
    for (i = 0; i < 2; i++)
    {
    
    
        for (j = 0; j < 3; j++)
        {
    
    
            cout << "a[" << i << "]" << "[" << j << "]数据:" << a[i][j] << endl;
        }
    }
    cout << endl;

    //指针访问二维数组-1
    cout << "指针访问二维数组-1" << endl;
    for (i = 0; i < 2; i++)
    {
    
    
        //对 *(*(a+i)+j) 的解释:
        //(a+i)指向的是存储一维数组地址(可理解为数组名)的地址
        //*(a+i)是将对应的数组名取出来
        //(*(a+i)+j)是一维数组每个元素的对应存储地址
        //*(*(a+i)+j)将对应的一维数组的元素数据取出
        for (j = 0; j < 3; j++)
        {
    
    
            cout << "a[" << i << "]" << "[" << j << "]数据:" << *(*(a+i)+j) << endl;
        }
    }
    cout << endl;

    //指针访问二维数组-2
    cout << "指针访问二维数组-2" << endl;
    for (p = a,i = 0; p < a+2; p++,i++)
    {
    
    
        for (j = 0; j < 3; j++)
        {
    
    
            cout << "a[" << i << "]" << "[" << j << "]数据:" << *(*p+j) << endl;
        }
    }
    cout << endl;
}

Put the TwodimensionalArray function in the main function to run, and the result is:
Two-dimensional array
in the pointer access array method 2, if the defined pointer is int *p, p=a cannot be set, because here p is associated with int type data. And what a is associated with is a one-dimensional integer array with a length of 3. Therefore, a pointer variable of the same type associated with a is defined here. After a is assigned to p, p++ operation can be performed on p, because p is a variable.

Summary: I still think this sentence is incisive: the array structure is recursive, and each element of an n-dimensional array is an n-1 dimensional array.

Array as parameter

Array name as function parameter

When the array name is used as a function parameter, C++ will do address transfer processing. When calling a function, the name of the shape parameter group accepts the address of the real parameter group, and the function indirectly accesses the real parameter group through the formal parameter pointer.

/**********************************************************
* 函数:PrintArray
* 描述:输出数组元素
* 参数:int x[] - 形参数数组名
        int length - 数组长度 
* 返回:无
* 备注:
**********************************************************/
void PrintArray(int x[], int length)
{
    
    
    int i;
    cout << "数组的元素为:" << endl;
    for (i = 0; i < length; i++)
    {
    
    
        cout << x[i] << " ";
    }
    cout << endl;
}
int a[] = {
    
    1,3,4,5,6,75,4,3};
int main()
{
    
    
	PrintArray(a,sizeof(a)/ sizeof(int));
 }

The parameter int x[] of the PrintArray function is the name of the parameter array, a passes its address to the parameter, and the elements of the array a can be accessed through the parameter in the function.

Reference materials: C++ programming foundation (Zhou Airu Lin Weijian)

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/105335117