Since a pointer is a data type, it should also have a corresponding operation or an operation, just as an integer can do subtraction. But every action or operation should have meaning for this data type. For example, two real numbers can be used in relational operations to know which is the larger which is smaller, and two imaginary number can not use the relational operation, because comparing the size of imaginary number is meaningless.
For pointer types, the operations you can use are: and integers do addition operations, and integers do subtraction operations, two pointers to do relational operations. Obviously, the pointer type multiplication and division method is meaningless, also is not allowed.
The addition and subtraction operation of pointers
The addition and subtraction of pointers and subtraction are different. We believe that pointers can only be combined with integers to add and subtract operations (including and integer constants, variables to do addition and subtraction and self reduction). In fact, this is not difficult to understand, memory storage space is based on "one" calculation, will not appear half of the storage space. So does the addition and subtraction of pointers do add and subtract to address values? Let's write a program to verify the result of the pointer Plus subtraction: (Procedure 8.3)
#include "iostream.h"
int main ()
{
int a[5]={1,2,3,4,5};
int *aptr=&a[0];//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;// The address stored in the output pointer and the data
aptr=aptr+i;//pointer and integer variable of the address are additive
} return
0;
Run Result:
(0x0012ff6c) =1 (
0x0012ff70) =2
(0x0012ff74) =3
(
0x0012ff78) =4 (0x0012ff7c) =5
We found that after each addition, the address value is not 1, but the difference is 4. So the addition of pointers and integers does not simply add an address value to an integer. We also found that after each addition, we were able to output the next element of the original pointer. Depending on how the array is stored in memory it is not difficult to draw the conclusion that the addition and subtraction of pointer and integer c is the pointer to move forward or backward to the corresponding type of storage area of C, that is, you can get the following formula:
New address = old address ±c* the number of bytes in memory for each variable of the corresponding data type
Because each int variable occupies 4 bytes in memory, the new address = old address +1*4= old address +4, each time the addition is finished in the program 8.3.1. As shown in Figure 8.3 on the right.
The relational operation of the pointer
We know that relational operations are equal, greater than, less than, greater than or equal, less than or equal to, and not equal to six kinds. For pointers, equal to and not equal to determine whether the values of two pointers are the same or different, that is, whether two pointers point to the same or different places. and greater than and small then judge the value of the pointer which is smaller. The value of the smaller in the memory position in the front, the value of the larger in the memory position in the back.
The above mentioned is the entire content of this article, I hope you can enjoy.