Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                                                                                            
                
        
    int **z[DIM0] = {yr,ys};

____210________218_______226________234________242________
|         |         |          |          |           |
|  218    | 226     |          |          |           |
|_________|_________|__________|__________|___________|_____
     yr       *yr      xa[0]      xa[1]        xa[2]  ........
                       ** yr

Hello,
I am trying to figure out logic of why type pointer to pointer int were used for array z elements type?

What happens when yr is assigned a type pointer to pointer int when array z is initialized. yr is and array of pointers to int. yr decays to pointer of int.
before array z is initialized.
But after z initializes yr is a pointer to pointer. Its no longer a pointer of type int due to decay? yr has and address what would address be after its converted to pointer to pointer of int as array z element. Could someone show memory diagram for yr, *yr, **yr?

after array z initialization after yr declaration

____200______208_____216_______________256________________
|         |       |         |         |               |
|         |   256 |         |         |      yr[0]    |
|_________|_______|_________|_________|_______________|________
|    yr      *yr     **yr                                                                       

confused I understand when you assign a type pointer to pointer to a pointer value. I understandhow memory is mapped. Confused because yr has and address before initialization.How is **yr created?

The following I follow.

int x = 5;
int *p = &x;
int **q = &p


___________240__________248________256_______
|                    |            |          |
|       248          |       256  |    5     |
|___________________ |___________ |__________|
         q                p             x
const int DIM = 2, DIM1 = 3, DIM2 = 4;
int xa[DIM2], xb[DIM2], xc[DIM2], xd[DIM2], xe[DIM2], xf[DIM2];
int *yr[DIM1] = {xa, xb, xc}, *ys[DIM1] = {xd, xe, xf};
**z[DIMO] = {yr, ys};

最佳答案

如果将所有数据分配为一个大块,如下所示:

int z[DIM1][DIM2][DIM3];


它会分配一个连续的数组,您可以
直接参考:

val= z[1][2][3];


这就是您的图所描述的。仅当z[][][]
一口气就被分配了。

但是事实并非如此,您的图表无法反映代码。

它被分配为许多较小的子集(xa,xb,xc,xd,xe,xf)
散落在记忆中。这就是为什么yr[]ys[]
指向每个集合开始的指针。 yr[0]指向xa[0]
yr[1]指向xb[0]yr[0]指向一个int,所以它是
声明为int *yr[]

现在yr和ys也可以独立分配。所以z[]需要
指向整数集的每个指针集。 z[0]
指向yr[0],它指向一个int的xa[0],因此z为
声明为int **z[]

如果周围散布着24个方框,则您的图表是正确的
在有很多箭头的页面上。

关于c++ - 指向4维数组的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31714030/

10-10 02:45