Arithmetic operations of pointers

Source: Internet
Author: User

C++arithmetic operation of the pointer C+ + pointers C + +The pointer pointer is an address that is represented by a numeric value. Therefore, you can perform arithmetic operations on the pointer. You can perform four arithmetic operations on pointers:+ + 、--、 +,-. Assuming that PTR is a pointer to an address +The integer pointer, is a +bit integer, let's perform the following arithmetic operation on the pointer: PTR++after performing the above operations, PTR will point to the location1004, because each time the PTR is incremented, it will point to the next integer position, where the current position is moved backward4A byte. This operation moves the pointer to the next memory location without affecting the actual value in the memory location. If PTR points to an address of +Character, the above operation causes the pointer to point to the position1001, because the next character position is in the1001. Increment a pointer we like to use pointers instead of arrays in our programs, because variable pointers can be incremented, and arrays cannot be incremented because the array is a constant pointer. The following program increments the variable pointer so that each element in the array is accessed sequentially: #include<iostream>using namespacestd;Const intMAX =3;intMain () {int  var[MAX] = {Ten, -, $}; int*ptr; //the array address in the pointerPTR =var;  for(inti =0; i < MAX; i++) {cout<<"Address of var["<< I <<"] = "; cout<< ptr <<Endl; cout<<"Value of var["<< I <<"] = "; cout<< *ptr <<Endl; //move to next positionptr++; }   return 0;} When the above code is compiled and executed, it produces the following result: Address ofvar[0] =0xbfa088b0Value ofvar[0] =TenAddress ofvar[1] =0XBFA088B4Value ofvar[1] = -Address ofvar[2] =0xbfa088b8Value ofvar[2] = $decrements A pointer Similarly, decrements the pointer by subtracting the value of its data type by the number of bytes, as follows: #include<iostream>using namespacestd;Const intMAX =3;intMain () {int  var[MAX] = {Ten, -, $}; int*ptr; //the address of the last element in the pointerPTR = &var[max-1];  for(inti = MAX; i >0; i--) {cout<<"Address of var["<< I <<"] = "; cout<< ptr <<Endl; cout<<"Value of var["<< I <<"] = "; cout<< *ptr <<Endl; //move to next positionptr--; }   return 0;} When the above code is compiled and executed, it produces the following result: Address ofvar[3] =0xbfdb70f8Value ofvar[3] = $Address ofvar[2] =0xbfdb70f4Value ofvar[2] = -Address ofvar[1] =0xbfdb70f0Value ofvar[1] =Tenpointer comparison pointers can be compared using relational operators, such as= =, < and >. If P1 and P2 point to two related variables, compared to different elements in an array, you can compare the size of P1 and P2. The following program modifies the above instance as long as the variable pointer points to an address that is less than or equal to the address of the last element of the array&var[MAX-1], the variable pointer is incremented: #include<iostream>using namespacestd;Const intMAX =3;intMain () {int  var[MAX] = {Ten, -, $}; int*ptr; //the address of the first element in a pointerPTR =var; inti =0;  while(PTR <= &var[MAX-1]) {cout<<"Address of var["<< I <<"] = "; cout<< ptr <<Endl; cout<<"Value of var["<< I <<"] = "; cout<< *ptr <<Endl; //point to previous locationptr++; I++; }   return 0;} When the above code is compiled and executed, it produces the following result: Address ofvar[0] =0xbfce42d0Value ofvar[0] =TenAddress ofvar[1] =0xbfce42d4Value ofvar[1] = -Address ofvar[2] =0xbfce42d8Value ofvar[2] = $

Arithmetic operations of pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.