# C++ 指针的算术运算

``````ptr++
``````

### 递增一个指针

``````#include <iostream>

using namespace std;
const int MAX = 3;

int main ()
{
int  var[MAX] = {10, 100, 200};
int  *ptr;

// 指针中的数组地址
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;

cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;

// 移动到下一个位置
ptr++;
}
return 0;
}
``````

``````Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
``````

### 递减一个指针

``````#include <iostream>

using namespace std;
const int MAX = 3;

int main ()
{
int  var[MAX] = {10, 100, 200};
int  *ptr;

// 指针中最后一个元素的地址
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;

cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;

// 移动到下一个位置
ptr--;
}
return 0;
}
``````

``````Address of var[3] = 0xbfdb70f8
Value of var[3] = 200
Value of var[2] = 100
Value of var[1] = 10
``````

### 指针的比较

``````#include <iostream>

using namespace std;
const int MAX = 3;

int main ()
{
int  var[MAX] = {10, 100, 200};
int  *ptr;

// 指针中第一个元素的地址
ptr = var;
int i = 0;
while ( ptr <= &var[MAX - 1] )
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;

cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;

// 指向上一个位置
ptr++;
i++;
}
return 0;
}
``````

``````Address of var[0] = 0xbfce42d0
Value of var[0] = 10