part1:指向一維char數(shù)組
char ch[40]="helloworld!";
#1:pch3是一個(gè)(char*)指針,它指向一個(gè)char, 即指向ch[40]的第一個(gè)元素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是一個(gè)(char*)指針痊末,它指向一個(gè)char, 即指向ch[40]的第一個(gè)元素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是一個(gè)char(*)[40]指針蚕苇,它指向一個(gè)含有40個(gè)char元素的數(shù)組,pch1+1跳過(guò)了40*sizeof(char)的長(zhǎng)度
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數(shù)組
??? char ch2[3][40]={
??????? "hello world1",
??????? "hello world2",
??????? "hello world3"
??? };
#1:ppch2是一個(gè)指向char的指針??
??? char* ppch2 = ch2[0];
??? cout << "*ppch2: "<< *ppch2 << endl;? //輸出的是'h'
#2:ppch3是一個(gè)指向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一維數(shù)組的指針和char一維數(shù)組的指針用法相同
??? 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:結(jié)構(gòu)體數(shù)組指針
??? 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:指針指向第一個(gè)元素
??? student* pstu1 = &pa[0];
??? cout << pstu1[0].fullname<< endl;
??? cout << pstu1->fullname<< endl;
??? cout <<(*(pstu1+2)).fullname << endl;
??? cout << pstu1[2].fullname<< endl;
#2:指針指向第一個(gè)元素
??? student* pstu = pa;
??? cout << pstu[0].fullname<< endl;
??? cout << pstu->fullname<< endl;
??? cout << (*(pstu+2)).fullname<< endl;
??? cout << pstu[2].fullname<< endl;
#3:指向整個(gè)結(jié)構(gòu)體數(shù)組
??? student(* pstu3)[3] = &pa;