您的位置:首页 > 编程语言 > C语言/C++

C++实例---数组访问(指针)

2017-02-15 15:52 218 查看
代码:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main (int argc, char **argv)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int i = 1, j = 1;
int (*p)[3];
cout<<"i = "<<i<<", j = "<<j<<endl;
for (int k = 0; k < 3; k ++)
{
for (int w = 0; w < 3; w ++)
cout<<setw(5)<<a[k][w];
cout<<endl;
}

p = a;
cout<<"*(*(a+i)+j) = "<<*(*(a+i)+j)<<endl;
cout<<"*(a+i)[j] = "<<*(a+i)[j]<<endl;
cout<<"*(a+3*i+j) = "<<*(a+3*i+j)<<endl;    //overstep the boundary
cout<<"*(*(p+i)+j) = "<<*(*(p+i)+j)<<endl;
cout<<"*(p[i]+j) = "<<*(p[i]+j)<<endl;
cout<<"*(p+i)[j] = "<<*(p+i)[j]<<endl;
cout<<"*(p+3*i+j) = "<<*(p+3*i+j)<<endl;    //overstep the boundary
cout<<"p[i][j] = "<<p[i][j]<<endl;

return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息