1. The significance of the array parameter degradation as a pointer
(1) The C language only passes the parameter as a copy of the value, and when passing an array to the function, copying an entire array of incoming functions results in inefficient execution, and the C language is designed to be efficient as the original design goal, so this approach 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 inta[][] 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.
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;}
Resources:
Www.dt4sw.com
Http://www.cnblogs.com/5iedu/category/804081.html
C Language Learning notes--array parameters and pointer parameters