再多些理解多维数组吧

来源:互联网 发布:阿里云 源代码管理 编辑:程序博客网 时间:2024/05/17 07:34

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 int ap[2][3][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
 int (*p)[3][5] = ap;
 int (*p1)[5]= &ap[1][1]; 
 int (*r)[5] = ap[0];
 int *t = ap[0][0];

 cout<<"a[2]"<<*(*(*(p+1)+0))<<endl;
 cout<<ap[1][0][0]<<endl;

 cout<<sizeof(ap)<<endl;

 cout<<**p1<<endl;
 printf("r=%x %d\n", r, **r);
 printf("++r=%x %d\n", ++r, **r);
 printf("t=%x %d\n", t, *t);
    printf("++t=%x %d\n", ++t, *t);
 printf("++t=%x %d\n", ++t, *t);

 system("pause");
 return 0;
}

 

总结: 多维数组拆解应从最左的下标开始,一定要注意数组首元素地址和元素值的区别,例如int (*p1)[5]= &ap[1][1]; 
 这里ap[1][1]是值,所以用&;而int (*r)[5] = ap[0];ap[0]是数组首元素地址,故不用&。

原创粉丝点击