8.1 获取int 和 double的变量地址
#include <iostream>
using namespace std;
int main()
{
int age = 30;
const double Pi = 3.1416;
cout << "Integer age is located at: 0x" << &age << endl;
cout << "Double Pi is located at: 0x" << &Pi << endl;
return 0;
}
/*
* 输出
Integer age is located at: 0x00D4F8E4
Double Pi is located at: 0x00D4F8D4
*/
8.2 声明并初始化指针
#include <iostream>
using namespace std;
int main()
{
int age = 30;
int* pointsToInt = &age;
cout << "Integer age is located at: 0x" << hex << pointsToInt << endl;
return 0;
}
/*
* 输出
Integer age is located at: 0x0116FC40
*/
8.3 给指针重新赋值,使其指向另一个变量
#include <iostream>
using namespace std;
int main()
{
int age = 30;
int* pointsToInt = &age;
cout << "PointsToInt points to age now\n";
cout << "PointsToInt = 0x" << hex << pointsToInt << endl;
int dogsAge = 9;
pointsToInt = &dogsAge;
cout << "PointsToInt points to dogsAge now\n";
cout << "PointsToInt = 0x" << hex << pointsToInt << endl;
return 0;
}
/*
* 输出
PointsToInt points to age now
PointsToInt = 0x0074FECC
PointsToInt points to dogsAge now
PointsToInt = 0x0074FEB4
*/
使用解除引用运算符(*)来访问整数值
#include <iostream>
using namespace std;
int main()
{
int age = 30;
int dogsAge = 9;
cout << "Integer age = " << age << endl;
cout << "Integer dogsAge = " << dogsAge << endl;
int* pointsToInt = &age;
cout << "PointsToInt points to age\n";
cout << "PointsToInt = 0x" << hex << pointsToInt << endl;
cout << "*PointsToInt = " << dec << *pointsToInt << endl;
pointsToInt = &dogsAge;
cout << "PointsToInt points to dogsAge now\n";
cout << "PointsToInt = 0x" << hex << pointsToInt << endl;
cout << "*PointsToInt = " << dec << *pointsToInt << endl;
return 0;
}
/*
* 输出
Integer age = 30
Integer dogsAge = 9
PointsToInt points to age
PointsToInt = 0x0027FAF0
*PointsToInt = 30
PointsToInt points to dogsAge now
PointsToInt = 0x0027FAE4
*PointsToInt = 9
*/
8.5使用指针和解除引用运算符操纵数据
#include <iostream>
using namespace std;
int main()
{
int dogsAge = 30;
cout << "Initialized dogsAge = " << dogsAge << endl;
int* pointsToAnAge = &dogsAge;
cout << "pointsToAnAge points to dosAge\n";
cout << "Enter an age for your dog:";
cin >> *pointsToAnAge;
cout << "Input stored at 0x" << hex << pointsToAnAge << endl;
cout << "Integer dogsAge = " << dec << dogsAge << endl;
return 0;
}
/*
* 输出
Initialized dogsAge = 30
pointsToAnAge points to dosAge
Enter an age for your dog:10
Input stored at 0x00B3F7E0
Integer dogsAge = 10
*/
8.11数组变量是指向第一个元素的指针
#include <iostream>
using namespace std;
int main()
{
int myNumbers[5];
int* pointToNums = myNumbers;
cout << "PointToNums = 0x" << hex << pointToNums << endl;
cout << "&myNumbers = 0x" << hex << &myNumbers[0] << endl;
return 0;
}
/*
* 输出
PointToNums = 0x001BFCDC
&myNumbers = 0x001BFCDC
*/
8.12 使用解除引用运算符(*)访问数组中的元素以及将数组运算符([])用于指针
#include <iostream>
using namespace std;
int main()
{
const int ARRAY_LEN = 5;
int myNumbers[ARRAY_LEN] = { 24, -1, 365, -999, 2011 };
int* pointToNums = myNumbers;
cout << "Display array using pointer syntax, operator*" << endl;
for (int index = 0; index < ARRAY_LEN; ++index)
{
cout << "Element " << index << " = " << myNumbers[index] << endl;
}
cout << "Display array using ptr with array syntax, operator[] \n";
for (int index = 0; index < ARRAY_LEN; ++index)
{
cout << "Element " << index << " = " << pointToNums[index] << endl;
}
return 0;
}
/*
* 输出
Display array using pointer syntax, operator*
Element 0 = 24
Element 1 = -1
Element 2 = 365
Element 3 = -999
Element 4 = 2011
Display array using ptr with array syntax, operator[]
Element 0 = 24
Element 1 = -1
Element 2 = 365
Element 3 = -999
Element 4 = 2011
*/
8.18 一个计算平方值并通过引用参数返回结果的函数
#include <iostream>
using namespace std;
void GetSquare(int& number)
{
number *= number;
}
int main()
{
cout << "Enter a number you wish to square: ";
int number = 0;
cin >> number;
GetSquare(number);
cout << "Square is: " << number << endl;
return 0;
}
/*
* 输出
Enter a number you wish to square: -2
Square is: 4
*/