数组指针
double(*a)[3][6];
//a----------double(*)[3][6] 数组指针(指向一个二维数组)
//*a---------double[3][6] double(*)[6] 数组指针(行指针,指向一个一维数组)
//**a--------double[6] 元素指针
//***a-------double
cout << sizeof(a) << endl;//8
cout << sizeof(*a) << endl;//3*6*8=144
cout << sizeof(**a) << endl;//6*8=48
cout << sizeof(***a) << endl;//8
double* i[3][6];
//i----------double* [3][6] 数组指针(指向一个二维数组,数组内存放指向double的指针)
//*i-------- double* [6] 数组指针(行指针,指向一个一维数组)
//**i--------double* 元素指针
//***i-------未知?
cout << sizeof(i) << endl;//3*6*8=144
cout << sizeof(*i) << endl;//6*8=48
cout << sizeof(**i) << endl;//8
cout << sizeof(***i) << endl;//8
double b[3][6];//b的类型是double[3][6],是行指针
cout << sizeof(b) << endl;//144
double(*p1)[6] = b;//p1的类型是double(*)[6]
cout << sizeof(*p1) << endl;//6*8
double c[6];//c的类型是double[6],是元素指针
cout << sizeof(c) << endl;//48
double(*p2)[6] = &c;//元素指针转换为行指针
//所以*p2是一个元素指针
动态内存分配的例子(二重指针):
int** a = new int* [3];//a是一个二重指针(其实a就是指针数组的数组名)
for (int i = 0; i < 3; i++)
{
a[i] = new int[4]();
}
int* b[3];//指针数组
b[0] = a[0];
b[1] = a[1];
b[2] = a[2];
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 3; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
// 0 0 0 0
// 0 0 0 0
// 0 0 0 0
for (int i = 0; i < 3; i++)
delete[] a[i];
delete[]a;
int a[2][3] = { { 1,2,3 },{ 4,5,6 } };
int* b[3];//指针数组,数组中的每个元素的类型都是int*
b[0] = a[0];//a[0]等价*a,是元素指针,类型是int*
b[1] = a[1];
b[2] = a[2];
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 3; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
// 1 2 3 4
// 4 5 6 - 858993460
// - 858993460 - 858993460 - 858993460 - 858993460