如何在C语言中声明一个二维数组的指针?
二维指针数组是一个拥有指针类型的变量的数组。这意味着,存储在二维数组中的变量是这样的:每个变量都指向其他一些元素的特定地址。
如何创建一个二维指针数组。
一个二维指针数组可以按照下面的方式创建。
int *arr[5][5]; //创建一个5行5列的2D整数指针阵列。
二维数组的元素是通过分配一些其他元素的地址来初始化的。
在这个例子中,我们将整数变量**'n'**的地址分配到了二维指针数组的索引(0, 0)中。
int n; //声明了一个变量
arr[0][0] = &n; //在位置(0,0)上分配了一个变量。
下面是二维指针数组的实现。
C
#include <stdio.h>
// Drivers code
int main()
{
int arr1[5][5] = { { 0, 1, 2, 3, 4 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 3, 2, 6 },
{ 2, 5, 4, 3, 1 } };
int* arr2[5][5];
// Initialising each element of the
// pointer array with the address of
// element present in the other array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arr2[i][j] = &arr1[i][j];
}
}
// Printing the array using
// the array of pointers
printf("The values are\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", *arr2[i][j]);
}
printf("\n");
}
return 0;
}
输出
The values are
0 1 2 3 4
2 3 4 5 6
4 5 6 7 8
5 4 3 2 6
2 5 4 3 1
