Simple analysis of C++ pointer operations and operations

  • 2020-04-02 03:13:35
  • OfStack

Since a pointer is a data type, it should have a corresponding operation or operation, just as an integer can do addition, subtraction, multiplication and division. But every operation or operation should make sense for this data type. For example, two real Numbers can be used to figure out which one is bigger and which one is smaller, while two imaginary Numbers cannot be used to figure out which one is bigger, because it makes no sense to compare imaginary Numbers.

For pointer types, the operations you can use are: add with integers, subtract with integers, and do relational operations with two Pointers. Obviously, multiplication and division of pointer types is meaningless and not allowed.

The addition and subtraction of Pointers

The addition and subtraction of Pointers are different from the addition and subtraction of Numbers. We believe that Pointers can only add and subtract from integers (including and integer constants, variables add and subtract, and add and subtract). In fact, it is not difficult to understand, memory storage space is calculated by "a", will not appear half of the storage space. So does the pointer add or subtract to the address value? Let's write a program to verify the result of pointer addition and subtraction :(program 8.3)


#include "iostream.h"
int main()
{
  int a[5]={1,2,3,4,5};
  int *aptr=&a[0];//Gives the address of the first element of the array to the pointer
  int i=1;
  for (int j=0;j<5;j++)
  {
   cout <<'(' <<aptr <<")=" <<*aptr <<endl;//Output the address stored in the pointer and the data for that address
   aptr=aptr+i;//Pointers and integer variables add
  }
  return 0;
}

Operation results:


(0x0012FF6C)=1
(0x0012FF70)=2
(0x0012FF74)=3
(0x0012FF78)=4
(0x0012FF7C)=5

We found that after each addition, the address values did not differ by 1, but by 4. So adding Pointers and integers is not simply adding address values and integers. We also found that after each addition, we were able to output the next element of the original pointer. According to the storage of the array in memory, it is not difficult to draw such a conclusion: the addition and subtraction method of pointer and integer C is to move the pointer forward or backward by C corresponding types of storage area, that is, the following formula can be obtained:
      New address = old address Plus or minus C* the number of bytes in memory for each variable corresponding to the data type

Because each int variable takes up 4 bytes in memory, the new address = the old address +1*4= the old address +4 for each addition in 8.3.1. As shown in figure 8.3 on the right.

Relational operation of Pointers

We know that there are six kinds of relational operations: equal, greater than, less than, greater than or equal to, less than or equal to, and not equal to. For Pointers, equal and not equal is to determine whether the values of two Pointers are the same or different, that is, whether the two Pointers point to the same or different places. Greater than and smaller then we can determine which value of the pointer is bigger and which value is smaller. The position of the smaller value in memory is higher, and the position of the larger value in memory is lower.

The above is all the content of this article, I hope you can enjoy it.


Related articles: