(Please don’t dizzy) Assign a value to an array using a pointer to a multidimensional array

#include <iostream>
using namespace std;
int main(){
    
    
int a[10]={
    
    0},b[10][10]={
    
    0},c[10][10][10]={
    
    0};
//申明指向整型变量的指针,指向a[0]
int *pa=a;
//申明指向长度为的整型数组的指针,指向了b[0]
int (*pb)[10]=b;
//申明指向大小为*10的二维整型数组的指针,指向了c[0]
int (*pc)[10][10]=c;
*(pa+1)=10; //给a[1]赋值
// pa 是 a[0] 地址,一次认一个存储空间,加 1 后到 a[1] 的地址,加 * 为 a[1] 的值 
*(*(pb+1)+1)=10; //给b[1][1]赋值
// pb 是 b[0] 地址,一次认一排存储空间,加 1 后到 b[1] 的地址,加 * 为 b[1] 的值(相当于一个一维数组),再加 1 到 b[1][1] 地址处,加 * 为 b[1][1] 的值
*(*(*(pc+1)+1)+1)=10; //给c[1][1][1]赋值
cout<<a[1]<<" "<<b[1][1]<<" "<<c[1][1][1]<<endl;
//依据上面两个分析,俄罗斯套娃一样,一次看一面到看一排到看一个,就不难理解了
return 0;
}
输出:10 10 10

Description: The storage structure of the array: One-dimensional is equivalent to rows of elements, two-dimensional is equivalent to each row being stacked into rectangular planes from top to bottom, and three-dimensional is equivalent to each rectangular plane being stacked from front to back into a rectangular parallelepiped. The order of access is From front to back in space, from left to right on the plane, and from top to bottom

Guess you like

Origin blog.csdn.net/interestingddd/article/details/114933835