#include <iostream>
using namespace std;
void main()
{
int a[3][4]={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
int **p=(int**)&a;
// p的作用不等效於a,所以無法對p這樣引用 p[?][?]
cout<<"a: "<<a<<endl;
cout<<"a[0]: "<<a[0]<<endl;
cout<<"a[1]: "<<a[1]<<endl;
cout<<"a[2]: "<<a[2]<<endl;
cout<<"p: "<<p<<endl; // p = a (address value)
// p所控制的大小是 sizeof(int)
// *p所控制的大小是 sizeof(int) * 4
cout<<(int)(*p)<<endl; // 1 -> a[0][0]
cout<<(int)(p[1])<<endl; // 2 -> a[0][1]
cout<<(int)(p[2])<<endl; // 3 -> a[0][2]
cout<<(int)((*p+1))<<endl; // 5 -> a[1][0]
cout<<(int)((*p+2))<<endl; // 9 -> a[2][0]
// 不要這樣使用
/*
for(i=0;i<3;i++)
for(j=0;j<4;j++)
cout<<*(*(p+i)+j);
*/
}
指針的控制範圍
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.