part1:指向一维char数组
char ch[40]="helloworld!";
#1:pch3是一个(char*)指针,它指向一个char, 即指向ch[40]的第一个元素ch[0]
char* pch3 = &ch[0];
cout << "pch3: "<< pch3 << " *pch3: " << *pch3 << "*(pch3+1): " << *(pch3+1) << endl;
cout << "pch3[0]:" << pch3[0] << " pch3[1] " << pch3[1]<< endl;
#2:pch是一个(char*)指针,它指向一个char, 即指向ch[40]的第一个元素ch[0]
char* pch = ch;
cout << (int*) pch <<" " << &ch <<" " << (int* )&ch[0]<< endl;
cout << pch << endl << " *pch : " << *pch << endl << " *( pch + 1 ) : " << *(pch + 1) << " pch[0]: " << pch[0] << " pch[1]: " << pch[1] << endl ;
#3:pch1是一个char(*)[40]指针,它指向一个含有40个char元素的数组,pch1+1跳过了40*sizeof(char)的长度
char (*pch1)[40] = &ch;
cout <<"pch1 + 1 : " << pch1+1 << " *( pch1 + 1 ) : " << * (pch1+1) << endl; //*(pch1+1)显示乱码
cout << "pch1[0] " << pch1[0] << " pch1[1]: " << pch1[1] << endl;
//输出:pch1[0] hello world! pch1[1]: 乱码
part2:指向二维char数组
char ch2[3][40]={
"hello world1",
"hello world2",
"hello world3"
};
#1:ppch2是一个指向char的指针
char* ppch2 = ch2[0];
cout << "*ppch2: "<< *ppch2 << endl; //输出的是'h'
#2:ppch3是一个指向char[40]的指针
char(* ppch3)[40] = ch2;
cout << "*ppch3: "<< *ppch3 << endl; //*ppch3:hello world1
#3:ppch4是指向char[3][40]的指针
char(* ppch4)[3][40] = &ch2;
cout << "*ppch4: "<< *ppch4 << endl;
part3:int一维数组的指针和char一维数组的指针用法相同
intintarray[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int* pint = intarray ;
cout << intarray[4] <<" " << pint[4] << endl;
part4:结构体数组指针
const int SLEN = 30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
student pa[3]={
{"MarkMa","basketball",1},
{"LitterLi","swimming",0},
{"YelloYe","piano",2}
};
#1:指针指向第一个元素
student* pstu1 = &pa[0];
cout << pstu1[0].fullname<< endl;
cout << pstu1->fullname<< endl;
cout <<(*(pstu1+2)).fullname << endl;
cout << pstu1[2].fullname<< endl;
#2:指针指向第一个元素
student* pstu = pa;
cout << pstu[0].fullname<< endl;
cout << pstu->fullname<< endl;
cout << (*(pstu+2)).fullname<< endl;
cout << pstu[2].fullname<< endl;
#3:指向整个结构体数组
student(* pstu3)[3] = &pa;