Array parameters and pointer parameters

1. In C language, parameters are only passed by value copy

2. When passing an array to a function, treat the array name as a constant pointer and pass the address of the first element of the array

3. The two-dimensional array parameter also has the problem of degradation. The two-dimensional array can be regarded as a one-dimensional array, and each element in the two-dimensional array is a one-dimensional array.

4. The parameter of the first dimension in the two-dimensional array parameter can be omitted

5. It is not possible to pass arbitrary multidimensional arrays to a function in C

6. In order to provide correct pointer arithmetic, all dimension lengths except the first must be provided

7. Restriction: One-dimensional array parameter - must provide a length information that marks the end of the array

        2D array parameter - cannot be passed directly to function

        3 or more dimension array parameter - cannot be used

#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));        for(i=0; i<row; i++)    {        for(j=0; j<col; j++)        {            printf("%d\n", a[i][j]);        }    }}int main(){    int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};        access(a, 3);}


Guess you like

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