Original address: 46565577
I. INTRODUCTION 1, two-dimensional array
int array[10][10];
function declaration: void fun (int a[][10])
Function call: Fun (array);
Access: General use of a[i][j] to access elements in an array
2. Array of pointers
int *array[10];
function declaration: void fun (int *a[10]);
Function call: Fun (array);
Access: Use * (A[I]+J) to access elements in an array
3. Pointer to pointer
int **array;
function declaration: void Fun (int **a);
Function call: Fun (array);
Access: * (* (a+i) + j) or A[i][j]? Access elements (access methods for two-dimensional arrays represented by double pointers)
Second, analysis of examples?
The following is a related topic:
- #Define ROW 2
- #define COL 3
- void myputs(char **pos);
- int main()
- {
- Char **p;
- Char a[row][col]={"abc", "def"};
- p = A;
- Myputs (P);
- return 0;
- }
- void myputs(char **p)
- {
- int I, J;
- For (i = 0; i < ROW; i++)
- {
- For (j = 0; j < COL; J + +)
- printf ("%c", P[i][j]);
- printf ("\ n");
- }
- }
At first glance, this topic will print out characters. But in fact, this program will run with a segment error.
Cause: When a two-dimensional array is passed as a double pointer, the array's head pointer is passed to the function, and its internal structure is not clear whether it will be properly accessed.
Correct:
Method one: Change void myputs (char **p) to void Myputs (char p[row][col])
Method Two: Change char A[row][col] to char *a[col]
The first method is well understood and certainly feasible. Let's take a look at method two.
In "c Expert programming", the issue of the parameter transfer process is described, the following is related to our topic content
How are array and pointer parameters modified by the compiler?
The "array name is rewritten as a pointer parameter" rule is not recursively defined. Arrays of arrays are rewritten as "pointers to arrays" instead of "pointer pointers":
Parameters that are matched by an argument
Array of array char c[8][10]; CHAR (*) [10]; Array pointers
Pointer array char *c[10]; Char **c; Pointer to pointers
Array pointer (row pointer) char (*C) [10]; char (*C) [10]; does not change
Pointer to the pointer char **c; Char **c; does not change
From the above, the parameters for char **c can be matched by Char **c and Char *c[10].
Why Char **c can match char *c[10], can't match char c[10][10]? This is related to the memory distribution of the two.
1, two-dimensional array of memory distribution:
Once a two-dimensional array is defined, a logically contiguous block of memory is allocated in memory. Char c[10][10], the system allocates a piece of contiguous memory of 100 bytes. In other words, such a two-dimensional array with a one-dimensional array char c[100] has a similar memory distribution. Below we use the program to verify:
- #Define ROW 2
- #define COL 3
- Union u
- {
- Char A[row][col];
- Char B[row*col];
- }u;
- int main()
- {
- int I, J;
- char ch = ' a ';
- For (i = 0; i < ROW; i++)
- For (j = 0; j < COL; J + +)
- {
- U.A[I][J] = ch;
- ch++;
- }
- For (i = 0; i < ROW; i++)
- {
- For (j = 0; j < COL; J + +)
- printf ("%c-%c\t", u.a[i][j], U.b[i*col + j]);
- printf ("\ n");
- }
- return 0;
- }
It can be seen that we can access a two-dimensional array in a way that accesses an array.
The specific memory distribution is as follows:
This is why int a[5][] is not allowed and int a[][4] is allowed.
2, two-dimensional pointer memory distribution:
Use two-dimensional pointers to represent the general dynamic allocation of two-dimensional arrays:
- Char **a;
- int I, J;
- char ch = ' a ';
- A = (char * *)malloc (sizeof ( char *) * ROW)
- ASSERT (a);
- For (i = 0; i < ROW; i++)
- {
- A[i] = (char *)malloc (sizeof (char) *col);
- ASSERT (A[i]);
- }
- For (i = 0; i < ROW; i++)
- For (j = 0; j < COL; J + +)
- {
- A[I][J] = ch;
- ch++;
- }
As you can see, the memory distributions of the two-dimensional arrays and the double pointers are completely different, so they cannot be passed to each other, and if passed, a segment error occurs. But Char *a[3] can be because its memory distribution is the same as char**a.
Third, summary char **p and Char p[2][3] cannot pass arguments to each other because their specific memory distribution is different, so there is a segment error at run time. One more thing to note: A[i][j in a two-dimensional array] and a[i][j in a double pointer) mean something different. In a two-dimensional array of int a[10][10], a[i][j] refers to the number of elements in the first row of column J. In the double pointer int **a, A[I][J] refers to the first J element in the address to which the int * pointer is placed. i.e. * (* (a+i) +j).
Two-dimensional arrays and double pointers (RPM)