C array parameters and pointer parameters (31)

        We mentioned earlier that in the C language, array parameters degenerate into pointers. So why is this? In the C language, parameters are only passed by value copy. When passing an array to a function, instead of copying the entire array and passing it into the function, the array name is regarded as a constant pointer and the address of the first element of the array is passed.

        So when the C language was established, it was mainly used for the Unix operating system, and Unix had high requirements for efficiency. Therefore, the C language is designed with high efficiency as the initial design goal: a> When the parameter is passed, if the entire array is copied, the execution efficiency will be greatly reduced; b> The parameter is located on the stack, and a too large array copy will cause the stack to overflow . The function call stack is stored in a piece of memory. If the stack overflows, the function call will not be executed and the program will crash.

        The two-dimensional array parameter is also degenerate, it can be regarded as a one-dimensional array, and each element in it is a one-dimensional array. The parameter of the first dimension in the two-dimensional array parameter can be omitted, for example: void f(int a[5]) ==> void f(int a[]) ==> void f(int* a); void g(int a[3][3]) ==> void g(int [][3]) ==> void g(int (*a)[3]) ; then the one-dimensional array we usually call as a parameter will be It will degenerate into a one-dimensional pointer, and a two-dimensional array will not degenerate into a two-dimensional pointer, but an array pointer. So what kind of parameters will degenerate into two-dimensional pointers? We summarize the table below

array parameter
Equivalent pointer parameter
1D array: float a[5]
pointer: float* a
Array of pointers: int* a[5]
pointer to pointer: int** a
Two-dimensional array: char a[3][4]
Array pointer: char(*a)[4]

        We can see that the pointer array degenerates into a two-dimensional pointer only when it is used as a parameter. Then in C language, you cannot pass any number of multidimensional arrays to a function, you must provide all dimension lengths except the first dimension ; the dimension information other than the first dimension is used to complete the pointer operation, and the essence of the N-dimensional array is One-dimensional array, the elements are N - 1-dimensional arrays, and only the first dimension is variable for function parameters of multi-dimensional arrays. Let's take the code as an example for analysis, the code is as follows

#include <stdio.h>

void access(int a[][3], int row)
{
    int col = sizeof(*a) / sizeof(int);
    int i = 0;
    int j = 0;
    
    printf("sizeof(a) = %d\n", sizeof(a));
    printf("sizeof(*a) = %d\n", sizeof(*a));
    
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            printf("%d\n", a[i][j]);
        }
    }
    
    printf("\n");
}

intmain()
{
    int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    int aa[2][2] = {0};
    
    access(a, 3);
    access(aa, 2);
    
    return 0;
}

        We see that in the program, we define 3*3 and 2*2 two-dimensional arrays in lines 25 and 26, respectively, but the second dimension of the array parameter of the parameter in the access function is specified as 3. So our call to this function on line 29 will throw an error because the types don't match. Let's take a look at the compilation results

picture.png

        We saw that it reported a warning, which means that although the program is compiled, the result it allows is uncertain. We see that the aa array we defined is 2*2, but it prints out 6 numbers, which is regarded as 2*3. So we call it wrong.

        Let's look at the code of a three-dimensional array, the code is as follows

#include <stdio.h>

void access_ex(int b[][2][3], int n)
{
    int i = 0;
    int j = 0;
    int k = 0;
    
    printf("sizeof(b) = %d\n", sizeof(b));
    printf("sizeof(*b) = %d\n", sizeof(*b));
    
    for(i=0; i<n; i++)
    {
        for(j=0; j<2; j++)
        {
            for(k=0; k<3; k++)
            {
                printf("%d\n", a[i][j][k]);
            }
        }
    }
    
    printf("\n");
}

intmain()
{
    int aa[2][2] = {0};
    int b[1][2][3] = {0};
    
    access_ex(b, 1);
    access_ex(aa, 2);
    
    return 0;
}

        What we specified in the access_ex function is that the second dimension is 2 and the third dimension is 3. But the array aa we defined is not like this, let's look at the compilation resultpicture.png

        We see that the result of the second compilation is also indeterminate. Through the study of array parameters and pointer parameters in this section, the summary is as follows: 1. In C language, parameters are only passed by value copy, and array parameters must degenerate into pointers; 2. Multidimensional array parameters must provide a value other than the first dimension. All dimensions except the length; 3. The first dimension of the function parameter value for multi-dimensional arrays is variable.


         Welcome to learn C language together , you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657304&siteId=291194637