&QUOT值;多维数组指针+ 1"不匹配

&QUOT值;多维数组指针+ 1"不匹配

问题描述:

这里的问题是程序:

#include <stdio.h>
int main()
{   
    int apricot[2][3][5];
    int (*r)[5]=apricot[0];
    int *t=apricot[0][0];

    printf("%p\n%p\n%p\n%p\n",r,r+1,t,t+1);
} 

它的输出是:

# ./a.out 
0xbfa44000
0xbfa44014
0xbfa44000
0xbfa44004

我觉得T的维度的值应为5,因为t是最后一个维度和事实吻合(0xbfa44004-0xbfa44000 + 1 = 5)

I think t's dimension's value should be 5 because t is the last dimension,and the fact is matched(0xbfa44004-0xbfa44000+1=5)

不过的r维的值为 0xbfa44014-0xbfa44000 + 1 = 21,我想应该是3 * 5 = 15 ,因为3和5的最后两个维度,那么,为什么不同的是21?

But the r's dimension's value is 0xbfa44014-0xbfa44000+1=21,I think it should be 3*5=15,because 3 and 5 are the last two dimensions,then why the difference is 21?

研究是一个指向5 int数组。

r is a pointer to an array of 5 ints.

假设1 int是您的系统上的4个字节(从 T T + 1 ),然后在踩着这个指针1( R + 1 )指5 * 4 = 20个字节的增加。这就是你在这里。

Assuming 1 int is 4 bytes on your system (from t and t+1), then "stepping" that pointer by 1 (r+1) means an increase in 5*4 = 20 bytes. Which is what you get here.