[C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针

时间:2023-01-07 03:33:43
 int main(){
//单值指针(指向单个值得指针,类同普通变量)
int *px = new int;
*px = 100;
printf("%d",*px);
delete px; //多值指针(指向多个元素的指针,类同普通数组变量)
int *p = new int[2];
p[0] = 2;
p[1] = 99;
printf("%d %d\n",p[0],p[1]);//2 99
delete [] p; //二维指针(属于多维指针)
int **pp = new int * [3];//m:宽 (即 列数)
for(int i = 0;i < 3;i++){
pp[i] = new int [2];//列长(即 行数)
}
for(int i = 0;i < 3;i++){
delete [] pp[i];
} //三维指针(属于多维指针)
int ***ppp = new int ** [3];//假定数组第一维为m, 第二维为n, 第三维为h
for(int i = 0;i < 3;i++){
ppp[i] = new int * [2];
for(int j = 0; j < 2; j++){
ppp[i][j] = new int [4];
}
}
for( int i=0; i < 3; i++ ){
for( int j=0; j < 2; j++ ){
delete ppp[i][j];
}
delete ppp[i];
} //数组指针(又称为 行指针;本质:指针)
int array[2][3] = {1,2,3,4,5,6};
int (*lineP)[3] = array;
delete [] lineP[0];
delete [] lineP[1];
delete [] lineP[2]; //指针数组(指向多个指针)
int m = 100;
int *pA = &m;
int *pB = &m;
int *pC = &m;
int *pa[2];
pa[0] = pA;
pa[1] = pB;
printf("%d %d",*(pa[0]),*(pa[1]));//100 100 }