C/C++ 指针和数组
指針和數(shù)組基本等價的原因在于指針?biāo)阈g(shù)和C/C++內(nèi)部處理數(shù)組的方式。
#include <iostream> using namespace std;int main() {double wages[3] = {100.0, 200.0, 300.0};short stacks[3] = {3, 2, 1};double *pw = wages;short *ps = &stacks[0];cout << "pw = " << pw << ", *pw = " << *pw <<endl;pw = pw + 1;cout << "add 1 to the pw pointer:\n";cout << "pw = " << pw << ", *pw = " << *pw << "\n\n";cout << "ps = " << ps << ", *ps = " << *ps <<endl;ps = ps + 1;cout << "add 1 to the ps pointer\n";cout << "ps = " << ps << ", *ps = " << *ps <<"\n\n";cout << "access two elements with array notation\n";cout << "stacks[0] = " << stacks[0] << ", stacks[1] = " << stacks[1] <<endl;cout << "access two elements with pointer notation\n";cout << "*stacks = " << *stacks << ", *(stacks + 1) = " << *(stacks + 1) <<endl;cout << sizeof(wages) << " = size of wages array\n";cout << sizeof(pw) << " = size of pw pointer\n";return 0; }(1)?上面程序能運行的原因是C/C++將數(shù)組名解釋為地址。和所有的數(shù)組一樣,wages存在下面的等式:wages = &wages[0] = 數(shù)組中第一個元素的地址;。
(2)?將指針變量加1后,其增加的值等于指向的類型占用的字節(jié)數(shù)。pw指向double類型,因此對pw加1就會讓它的值增加8個字節(jié)。
(3) 從程序的輸出可知,*(stacks + 1)和stacks[1]是等價的。同樣,*(stacks + 2)和stacks[2]是等價的。
在很多情況下,可以以相同的方式使用指針名和數(shù)組名。區(qū)別之一是,可以修改指針的值,而數(shù)組名是常量:
pointername = pointername + 1; ????//合法 arrayname = arrayname + 1; ????????//不合法(4)另一個區(qū)別是,對數(shù)組應(yīng)用sizeof運算符得到的是數(shù)組的長度,而對指針應(yīng)用sizeof得到的是指針的長度,即使指針指向一個數(shù)組。
(5)數(shù)組名被解釋為其第一個元素的地址,而對數(shù)組名應(yīng)用地址運算符時,得到的是整個數(shù)組的地址:
short tell[10]; cout << tell << endl; ???//是第一個元素的地址 cout << &tell << endl; ??//是整個數(shù)組的地址從數(shù)字上看,兩個地址相同,但從概念上說,&tell[0]是一個2字節(jié)內(nèi)存塊的地址,而&tell是一個20字節(jié)內(nèi)存塊的地址。因此,表達(dá)式tell+1將地址值加2,而表達(dá)式&tell+2將地址加20。換句話說,tell是一個short指針(*short),而&tell是一個這樣的指針,即指向包含20個元素的short數(shù)組(short(*)[20])。
(6)使用指針的金科玉律:一定要在對指針應(yīng)用解除引用運算符(*)之前,將指針初始化為一個確定的、適當(dāng)?shù)牡刂贰?/span>
?
?
使用new創(chuàng)建動態(tài)結(jié)構(gòu)
使用new創(chuàng)建動態(tài)結(jié)構(gòu)的步驟:創(chuàng)建結(jié)構(gòu)和訪問成員。創(chuàng)建動態(tài)結(jié)構(gòu)時,不能將成員運算符句點用于結(jié)構(gòu)名,因為這種結(jié)構(gòu)沒有名稱,只是知道它的地址。C/C++專門為這種情況提供了一個運算符:箭頭成員運算符(->)。
另一種訪問結(jié)構(gòu)成員的方法是,如果ps是指向結(jié)構(gòu)的指針,則*ps就是被指向的值——結(jié)構(gòu)本身。由于*ps是一個結(jié)構(gòu),因此(*ps).price是該結(jié)構(gòu)體的price成員。C++的運算符優(yōu)先規(guī)則要求使用括號。
#include <iostream> using namespace std;struct asd {char name[20];float volume;double price; };int main() {asd *ps = new asd;cout << "Enter asd item: ";cin.get(ps->name, 20);cout << "Enter volume:";cin >> (*ps).volume;cout << "Enter price:";cin >> ps->price;cout <<endl;cout << "Name: " << (*ps).name <<endl;cout << "Volume: " << ps->volume << " cubic feet\n";cout << "Price: " << ps->price <<endl;delete ps;return 0; }?
使用new和delete的實例
#include <iostream> #include <cstring> using namespace std;char *getname(void);int main() {char *name;name = getname();cout << name << " at " << (int *)name <<endl;delete []name; //指針?biāo)赶虻目臻g被釋放,但指針還可以使用name = getname();cout << name << " at " << (int *)name <<endl;delete []name;return 0; }char *getname() {char temp[80];cout << "Enter last name: ";cin >> temp;char *pn = new char[strlen(temp) + 1];strcpy(pn, temp);return pn; }獲取空間后,getname()使用標(biāo)準(zhǔn)庫函數(shù)strcpy()將temp中的字符串復(fù)制到新的內(nèi)存快中,該函數(shù)并不檢查內(nèi)存塊是否容納字符串,但getname()通過使用new請求合理的字節(jié)數(shù)來完成這樣的工作。
?
二級指針實例
若A是指向指針的指針,則稱為二級指針。將用于存放二級指針的變量稱為二級指針變量。
#include <iostream> using namespace std;struct asd {int year; };int main() {asd s1,s2,s3;s1.year = 2017;asd *pa = &s2;pa->year = 2018;asd trio[3];trio[0].year = 2019;cout << trio->year <<endl;const asd *arp[3] = {&s1, &s2, &s3};cout << arp[1]->year <<endl;const asd **ppa = arp;cout << (*ppa)->year <<endl;cout << (*(ppa+1))->year <<endl;return 0; }由于ppa指向arp的第一個元素,因此*ppa為第一個元素(ppa是一個指向結(jié)構(gòu)指針的指針,因此*ppa是一個結(jié)構(gòu)指針),即&s1。所以,(*ppa)->year為s1的year成員(*ppa等價于arp[0],所以(*ppa)->year等價于arp[0]->year)。之前提到的,如果ps是指向結(jié)構(gòu)的指針,則*ps就是被指向的值——結(jié)構(gòu)本身。所以還可以這樣訪問arp[0]所指向的s1:
cout << (**ppa).year <<endl;?
使用數(shù)組區(qū)間
對于處理數(shù)組的函數(shù),必須將數(shù)組中的數(shù)據(jù)種類、數(shù)組的起始位置和數(shù)組中元素數(shù)量提交給它;傳統(tǒng)的C/C++方法是,將指向數(shù)組起始處的指針作為一個參數(shù),將數(shù)組長度作為第二個參數(shù)。另一種給函數(shù)提供信息的方法是指定元素區(qū)間,這可以通過傳遞兩個指針來完成,一個指針標(biāo)識數(shù)組開頭,另一個指針標(biāo)識數(shù)組的尾部。STL使用"超尾"概念來指定區(qū)間。也就是說,對于數(shù)組而言,標(biāo)識數(shù)組結(jié)尾的參數(shù)將是指向最后一個元素后面的指針。
#include <iostream> using namespace std;const int Asize = 8; int sum_arr(const int *begin, const int *end);int main() {int asd[Asize] = {1, 2, 4, 8, 16, 32, 64, 128};int sum = sum_arr(asd, asd + Asize);cout << "Total asd: " << sum <<endl;sum = sum_arr(asd, asd + 3);cout << "First three elements: " << sum <<endl;sum = sum_arr(asd + 4, asd + 8);cout << "Last four elements: " << sum <<endl;return 0; }int sum_arr(const int *begin, const int *end) {const int *pt;int total = 0;for(pt = begin; pt != end; pt++){total = total + *pt;}return total; }總結(jié)
以上是生活随笔為你收集整理的C/C++ 指针和数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ getline() 和 get(
- 下一篇: C/C++函数指针