1. The significance of the array parameter degradation as a pointer
(1) The C language will only pass the parameter as a copy of the value , when passing an array to the function, copying an entire array of incoming functions resulting in inefficient execution, the C language is efficient as the original design goal, so this method is undesirable.
(2) The parameter is on the stack, and too large a copy of the array will cause the stack to overflow.
(3) The array name is treated as a constant pointer, passing the first element address of the array, not the entire array.
2. Two- dimensional array parameters
(1) The two-dimensional array parameter also has the problem of degeneration :
A two-dimensional array can be considered a one-dimensional array in which each element is a one-dimensional array
(2) The parameters of the first dimension in the two- dimensional number dimension parameter can be omitted
①void f (int a[5]) ←→void f (int a[]) ←→void f (int* a)
②void g (int a[5][3]) ←→void g (int a[][3]) ←→void g (int (*a)[3]);
(3) Equivalence relation
Array parameters |
Equivalent pointer parameters |
Note |
One-dimensional array: float a[5] |
Pointer: float* A |
The equivalent of the array minus the 1th dimension, and then the array name preceded by *. |
Array of pointers: int* A[5] |
Pointer to pointers: int** A; |
Two-dimensional array: char a[3][4] |
Pointer to array: char (*A) [4] |
(4) Neglected points of knowledge
An arbitrary multidimensional array cannot be passed to one of the ①c languages. In other words, formal parameter int a[][3] is legal, but int a[][] is illegal.
② Therefore, all other dimension lengths except the 1th dimension must be provided . Dimension information outside the first dimension is used to complete pointer operations.
"Instance analysis" passing and accessing a two-dimensional array
#include <stdio.h>voidAccessinta[][3],intRow//A[][3], the length of the 2nd dimension of the formal parameter must be provided{ intCol =sizeof(*a)/sizeof(int); inti =0; intj =0; printf ("sizeof (a) =%d\n",sizeof(a));//pointer, 4 bytes//A is an int (*a) [3] type, that is, a points to an array with 3 elements of type intprintf"sizeof (*a) =%d\n",sizeof(*a));//3*4 = 12 bytes for(i =0; i < row; i++) for(j =0; J < Col; J + +) {printf ("%d\n", A[i][j]); } printf ("\ n");}voidACCESS_EX (intb[][2][3],intN//the 2nd, 3-dimensional length of the array parameter must provide{ inti =0; intj =0; intK =0; printf ("sizeof (b) =%d\n",sizeof(b));//pointer, 4 bytes//b is an int (*b) [2][3] type, that is, B points to a two-dimensional array with 2 rows of 3 column int elementsprintf"sizeof (*B) =%d\n",sizeof(*b));//2 * 3 * 4 = 24 bytes for(i =0; i < n; i++) for(j =0; J <2; J + +) for(k =0; K <3; k++) {printf ("%d\n", B[i][j][k]); } printf ("\ n");}intMain () {inta[3][3] = {{0,1,2},{3,4,5},{6,7,8}}; intaa[2][2] = {0}; intb[1][2][3] = {0}; Access (A,3); //access (aa,2);//Error,int (*) [2] does not match the INT (*) [3] Typeaccess_ex (b,1); //access_ex (aa,2);//Int (*) [2] does not match int (*) [2][3] Type return 0;}
3. Summary
(1) In C language, parameters are passed in the form of a value copy
(2) The array parameter in C language must degenerate into a pointer
(3) Multidimensional array parameters, all dimension lengths except the 1th dimension must be provided.
(4) For function parameters of multidimensional arrays, only the first dimension is mutable (i.e. not provided)
35th lesson array parameter and pointer parameter analysis