Latest web development tutorials

C ++ pointer arithmetic

C ++ pointers C ++ pointers

Pointer is the address of a numerical representation. Therefore, you can perform arithmetic operations on pointers. Pointer can be four-arithmetic operation: +, -, +, -.

Supposeptr is a pointer to an integer pointer address 1000, is a 32-bit integer, let the pointer arithmetic to perform the following:

ptr++

After performing the above operation,ptr points to position 1004, because ptr each additional time it will point to the next integer position that the current position moved back four bytes.In the case of this operation will not affect the actual value of the memory location, move the pointer to the next memory location. Ifptr points to an address of 1000 characters, the above operation will cause a pointer to the position of 1001, because the next character position in 1001.

Incrementing a pointer

We like to use a pointer instead of an array in the program, because the variable pointer is incremented, and the array can not increase, because the array is a constant pointer. The following program increments the variable pointer to sequentially access each element in the array:

#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;
}

When the above code is compiled and executed, it produces the following results:

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

Decrement a pointer

Similarly, the pointer is decremented and subtracting the number of bytes that is the value of its data type, as follows:

#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;
}

When the above code is compiled and executed, it produces the following results:

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

Compare pointer

Pointer can be used to compare the relationship between operators, such as ==, <and>. If p1 and p2 points to two related variables, such as with an array of different elements, it can be the size of p1 and p2 compared.

The following procedure modifies the above example, as long as the address of the last element of a variable pointer is the address of the array is less than or equal to & var [MAX - 1], put the pointer variable is incremented:

#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;
}

When the above code is compiled and executed, it produces the following results:

Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200

C ++ pointers C ++ pointers