Introduction: Two-dimensional arrays are stored in the form of one-dimensional arrays in memory, so two-dimensional arrays can be regarded as composed of several one-dimensional arrays.
1. Row address & column address
? Line address:
a ↔ & a [0] ( point to line 0 )
a + i ↔ & a [i] ( Direction line i )
? Column address:
a [0] ↔ & a [0] [0] ( point to row 0, column 0 )
a [0] + i ↔ & a [0] [i] ( Direction 0th row, ith column )
2, the row address? Column address
- The row address points to a row of elements (a one-dimensional array)
- The column address points to an element
3. The relationship between one-dimensional array and two-dimensional array
The address of the one-dimensional array ↔ the row pointer of the two-dimensional array, pointing to the elements of the row, such as & a [i] ↔ a + i
Elements of one-dimensional array ↔ Row array name of two-dimensional array, pointing to the first element of the row, such as a [i] ↔ & a [i] [0]
4、a?a[0]
a is the name of the two-dimensional array, the row pointer, increment by 1 to point to the next row
a [0] is the name of the row array, column pointer, incremented by 1 to point to the next column
? Line pointer
① Definition: int (* p) [4]; ( defines a pointer variable to point to an integer array containing 4 elements )
②Initialization: p = a; or p = & a [0];
③Quote
地址&a[i][j]→ *(p+i)+j
元素a[i][j]→ p[i][j] ↔ * (p[i]+j) ↔ * (* (p+i)+j) ↔ (*(p+i))[j]
? Column pointer
① Definition: int * p; (Take two-dimensional array as one-dimensional array with m rows and n columns)
②Initialization: p = * a; or p = a [0]; or p = & a [0] [0];
③Quote
地址&a[i][j] → &p[i*n+j]
元素a[i][j] → p[i* n+j] ↔ * (p+i*n+j)
Example: input a 2D array with 3 rows and 4 columns and output
Method 1-Two-dimensional array as parameter
#include <stdio.h>
#define N 4
void InputArray(int p[][N], int m, int n);
void OutputArray(int p[][N], int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
OutputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
return 0;
}
/* 形参声明为列数已知的二维数组,输入数组元素值 */
void InputArray(int p[][N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &p[i][j]);
}
/* 形参声明为列数已知的二维数组,输出数组元素值 */
void OutputArray(int p[][N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", p[i][j]);
printf("\n");
}
}
Method 2-Line pointer as a formal parameter
#include <stdio.h>
#define N 4
void InputArray(int (*p)[N], int m, int n);
void OutputArray(int (*p)[N], int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
OutputArray(a, 3, 4); /* 向函数传递二维数组的第0行的地址 */
return 0;
}
/* 形参声明为指向列数已知的二维数组的行指针,输入数组元素值 */
void InputArray(int (*p)[N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", *(p+i)+j);
}
/* 形参声明为指向列数已知的二维数组的行指针,输出数组元素值 */
void OutputArray(int (*p)[N], int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", *(*(p+i)+j));
printf("\n");
}
}
Method 3-Column pointer as a formal parameter (recommended, the number of two-dimensional columns is dynamically variable)
#include <stdio.h>
void InputArray(int *p, int m, int n);
void OutputArray(int *p, int m, int n);
int main()
{
int a[3][4];
printf("Input 3*4 numbers:\n");
InputArray(*a, 3, 4); /* 向函数传递二维数组的第0行第0列的地址 */
OutputArray(*a, 3, 4); /* 向函数传递二维数组的第0行第0列的地址 */
return 0;
}
/* 形参声明为指向二维数组的列指针,输入数组元素值 */
void InputArray(int *p, int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &p[i*n+j]);
}
/* 形参声明为指向二维数组的列指针,输出数组元素值 */
void OutputArray(int *p, int m, int n)
{
int i, j;
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%4d", p[i*n+j]);
printf("\n");
}
}