|
复制代码
int main()
{
? ? int iVar = 100;
? ? int *pInt = &iVar;
? ? cout<<(*pInt)<
?
? ? string strVar = "volcanol";
? ? string *pStr = &strVar;
? ? cout<<(*pStr)<
?
? ? vector vInt(1);
? ? vector *pVecInt=&vInt;
? ? cout<<(*pVecInt)[0]<
?
? ? bitset<5> bitVar(5);
? ? bitset<5> *pBitset5 = &bitVar;
? ? cout<< (*pBitset5) <
?
? ? return 0;
}
复制代码
程序的执行结果如下所示:
?
[root@localhost cpp_src]# g++ test.cpp?
[root@localhost cpp_src]# ./a.out?
100
volcanol
0
00101
要点:
?
在定义指针变量的时候,必须在每个指针变量的前面都加上 * ,否则定义的就是一个非指针变量。
?
int ?*pInt1,pInt2; //pInt1 为指针变量, ?pInt2为整型变量。
?
在定义指针变量的时候,有两种风格的格式: ? int ?*pInt 和 ?int* ?pInt; 这两种格式没有对错之分,两种格式 C++都是接受的,只是在理解的时候可能会引起
?
误解。为了避免误解,在一个程序里面,最好选取一种格式一直保持下去。
?
?
?
?
?
4、指针的指针
?
指针变量也是一种对象,同样可以给指针变量定义一个指向它的指针,就是指针的指针。定义语法如下:
?
指针的指针变量指向的对象类型 ?**指针的指针变量标识符;
?
Exp:
?
int iVar = 10 ;
?
int *pInt = &iVar;
?
int **ppInt = &pInt;
如上就定义了一个指向整型指针变量的指针变量ppInt; ? ppInt指向的对象的类型为 int* 类型的对象。
?
复制代码
int main()
{
? ? int iVar = 100;
? ? int *pInt = &iVar;
? ? int **ppInt = &pInt;
?
? ? cout <<"iVar ="<< iVar<
? ? cout <<"int *pInt = &iVar,then *pInt ="<<*pInt<
? ? cout <<"int **ppInt = &pInt,then *ppInt="<<*ppInt;
? ? cout <<";and then **ppInt="<<**ppInt<
?
? ? return 0;
}
复制代码
程序的执行结果如下所示:
?
[root@localhost cpp_src]# g++ test.cpp?
[root@localhost cpp_src]# ./a.out?
iVar =100
int *pInt = &iVar,then *pInt =100
int **ppInt = &pInt,then *ppInt=0xbfb949f8;and then **ppInt=100
?
?
5、通过指针访问数组元素
?
这里需要说明一个细节: ?某一个数组的数组名是一个常量,而且数组名表示的是数组的第一个元素的首地址,同时数组元素在内存中是连续存放的。
?
正是因为数组具有上述的特点,才能方便的通过指针来访问数组的元素。
?
通过指针访问数组元素的例子如下:
?
复制代码
int main()
{
? ? int iArray[5] = {1,2,3,4,5};
? ? int *pInt = iArray;
?
? ? cout << *pInt << endl; ?// 1
? ? cout << pInt[0]<
? ? cout << *++pInt<
? ? cout << *pInt++<
? ? cout << *pInt<
?
? ? return
}
复制代码
程序的执行结果如下所示:
?
[root@localhost cpp_src]# ./a.out?
1
1
2
2
3
不但可以通过++运算符来改变指针的指向,指针还支持加整数和减整数运算,同时支持两个指针的减法运算。
?
复制代码
int main()
{
? ? int iArray[5] = {1,2,3,4,5};
? ? int *pInt1 = iArray;
? ? int *pInt2= &iArray[4];
?
? ? cout <<*(pInt1 + 2)<
? ? cout <<*(pInt2 - 1)<
? ? cout << pInt2 - pInt1 <
?
? ? return 0;
}
复制代码
程序的执行结果如下:
?
[root@localhost cpp_src]# g++ test.cpp?
[root@localhost cpp_src]# ./a.out?
3
4
4
要点:
?
可以发现这个地方 ?pInt2 - pInt1 的结果是4, 这个结果与 C语言的输出是存在差别的。这一点要非常注意,在指针与数组结合使用的过程中,两个指针相减
?
是经常见到的操作,因此这个地方需要注意。
?
?
通过上面的实例,我们可知利用指针可以很方便的访问数组的元素,因此我们可以通过指针遍历整个数组。
?
复制代码
int main()
{
? ? int iArray[5] = {1,2,3,4,5};
?
? ? for(int *pBegin=iArray,*pEnd=iArray+5; pBegin != pEnd; ++pBegin)
? ? ? ? cout<<*pBegin<
?
? ? return 0;
}
复制代码
程序的执行结果如下所示:
?
复制代码
[root@localhost cpp_src]# g++ test.cpp?
[root@localhost cpp_src]# ./a.out?
1
2
3
4
5
复制代码
指针和数组之间的定义还包括* 和 [] 符号同时在定义中出现的情况,
?
Exp:
?
复制代码
?1 #include
?2 #include
?3 #include
?4 #include
?5?
?6 using std::cin;
?7 using std::cout;
?8 using std::endl;
?9 using std::string;
10 using std::vector;
11 using std::bitset;
12?
13 int main()
14 {
15 ? ? int iArray_1[5] = {1,2,3,4,5};
16 ? ? int iArray_2[3] = {1};
17 ? ? int *pInt1[5] ={iArray_1, iArray_2};
18 ? ? int (*pInt2)[5] = iArray_1; //error
19 ? ? pInt2 = iArray_2; ? //error?
20?
21?
22 ? ? return 0;
23 }
复制代码
? ? ?上面的代码中, 我标出了两处错误,错误的原因是, pInt2 是一个二维的指针,而iArray_1 和 iArray_2 都是int * 类型的指针, 如果将程序修改一下就可以
?
得到如下的结果。
?
复制代码
?1 #include
?2 #include
|