|
一、
读程序:
1#include<iostream> 2using namespace std; 3int main() 4{ 5 int a[3][3]={1,2,3,4,5,6,7,8,9}; 6 int (*p)[3],i=2,j=0; 7 cout<<"int a[3][3]=(1,2,3,4,5,6,7,8,9};"<<endl; 8 cout<<"int (*p)[3],i=2,j=0;"<<endl; 9 cout<<"-------------------------"<<endl; 10 cout<<"a[i][j]="<<a[i][j]<<endl; 11 cout<<"*(a[i]+j)="<<*(a[i]+j)<<endl; 12 cout<<"*(*(a+i)+j)="<<*(*(a+i)+j)<<endl; 13 p=&a[1]; 14 cout<<"--After 'p=&a[i];'--"<<endl; 15 cout<<"(*p)[0]="<<(*p)[0]<<endl; 16 cout<<"(*(p+0))[0]="<<(*(p+0))[0]<<endl; 17 cout<<"p[0][0]="<<p[0][0]<<endl; 18 cout<<"(*p)[1]="<<(*p)[1]<<endl; 19 cout<<"(*p+1)[0]="<<(*p+1)[0]<<endl; 20 cout<<"(*(p+1))[0]="<<(*(p+1))[0]<<endl; 21 cout<<"p[1][0]="<<p[1][0]<<endl; 22 p++; 23 cout<<"--After 'p++;'--"<<endl; 24 cout<<"(*p)[2]="<<(*p)[2]<<endl; 25 cout<<"p[0][2]="<<p[0][2]<<endl; 26 cout<<"*(p[0]+2)="<<*(p[0]+2)<<endl; 27 cout<<"*(*p+2)="<<*(*p+2)<<endl; 28 return 0; 29 } 30
运行结果:
int a[3][3]=(1,2,3,4,5,6,7,8,9}; int (*p)[3],i=2,j=0; ------------------------- a[i][j]=7 *(a[i]+j)=7 *(*(a+i)+j)=7 --After 'p=&a[i];'-- (*p)[0]=4 (*(p+0))[0]=4 p[0][0]=4 (*p)[1]=5 (*p+1)[0]=5 (*(p+1))[0]=7 p[1][0]=7 --After 'p++;'-- (*p)[2]=9 p[0][2]=9 *(p[0]+2)=9 *(*p+2)=9
二、 引用的建立及简单使用 读程序:
1#include<iostream> 2using namespace std; 3int main() 4{ 5 int i; 6 int &ri=i; 7 int *pi=&i; 8 cout<<"&i="<<&i<<endl; 9 cout<<"&ri="<<&ri<<endl; 10 cout<<"&pi="<<&pi<<endl; 11 i=1011; 12 cout<<"i="<<i<<",*pi="<<*pi<<",ri="<<ri<<endl; 13 *pi=2022; 14 cout<<"i="<<i<<",*pi="<<*pi<<",ri="<<ri<<endl; 15 ri=3033; 16 cout<<"i="<<i<<",*pi="<<*pi<<",ri="<<ri<<endl; 17 return 0; 18} 19
运行结果:
&i=0x22ff74 &ri=0x22ff74 &pi=0x22ff6c i=1011,*pi=1011,ri=1011 i=2022,*pi=2022,ri=2022 i=3033,*pi=3033,ri=3033
|