Arithmetic pointer in C / C ++
As explained in the main chapter, the cursor in C / C ++ is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer as you would with numeric values. There are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
As explained in the main chapter, the cursor in C / C ++ is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer as you would with numeric values. There are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
To understand arithmetic pointers, we assume that contro is an integer pointer that points to 1000 addresses. Assuming an integer of 32 bits, we perform the arithmetic operation on this pointer:
contro ++
Now, after the above operation, the contro will point to position 1004 because each contro is incremented by one, it will point to the next integer which is the next 4 bytes of the current position. This will move the cursor to the next memory location without affecting the actual value at the memory location. If contro points to a character whose address is 1000, then the above operation will point to position 1001 because the next character will be at 1001.
Increase a Cursor in C / C ++
We prefer to use a pointer in the program instead of using an array because the variable pointer can increase, unlike the array name, cannot be increased, because it is a constant pointer. The following program increases the variable pointer to access each element of the array:
#include using namespace std ; const int MAX = 3 ; int main () { int mang [ MAX ] = { 10 , 100 , 200 }; int * contro ; // bay gio chung ta co mot mang dia chi trong con tro. contro = mang ; for ( int i = 0 ; i < MAX ; i ++) { cout << "Dia chi cua mang[" << i << "] = " ; cout << contro << endl ; cout << "Dia chi cua mang[" << i << "] = " ; cout << * contro << endl ; // tro toi vi tri tiep theo contro ++; } return 0 ; }
Running the above C / C ++ program will produce the following results:
Reducing a Cursor in C / C ++
The following program to reduce a pointer, which will reduce its value by the number of bytes of the data type as follows:
#include using namespace std ; const int MAX = 3 ; int main () { int mang [ MAX ] = { 10 , 100 , 200 }; int * contro ; // bay gio chung ta co dia chi cua phan tu cuoi trong con tro. contro = & mang [ MAX - 1 ]; for ( int i = MAX ; i > 0 ; i --) { cout << "Dia chi cua mang[" << i << "] = " ; cout << contro << endl ; cout << "Dia chi cua mang[" << i << "] = " ; cout << * contro << endl ; // tro toi vi tri o truoc contro --; } return 0 ; }
Running the above C / C ++ program will produce the following results:
Compare Pointer in C / C ++
Pointers can be compared using relational operators, like ==, <, and>. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be compared meaningfully.
The following program modifies the previous example by increasing the variable pointer so that the address it points to is less than or equal to the address of the last element of the array, & var [MAX - 1]:
#include using namespace std ; const int MAX = 3 ; int main () { int mang [ MAX ] = { 10 , 100 , 200 }; int * contro ; // bay gio chung ta co dia chi cua phan tu dau tien trong con tro. contro = mang ; int i = 0 ; while ( contro <= & mang [ MAX - 1 ] ) { cout << "Dia chi cua mang[" << i << "] = " ; cout << contro << endl ; cout << "Dia chi cua mang[" << i << "] = " ; cout << * contro << endl ; // tro toi vi tri o truoc contro ++; i ++; } return 0 ; }
Running the above C / C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Cursor NULL in C / C ++
Next article: Cursor NULL in C / C ++
You've just finished reading the article "Arithmetic pointer in C / C ++" edited by the TipsMake team. You can save arithmetic-pointer-in-c-c-.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.
- Cursor this in C ++
- The way to teach children fast mental arithmetic of Japanese people
- Cursor in C
- How to use Head Pointer on a Mac
- How to fix the mouse pointer disappearing on Windows 10
- Cursor NULL in C / C ++
- How to change the mouse pointer on Windows 11 with many styles and colors
- How to change the mouse pointer size and style in Windows 11
- How to change the pointer sensitivity, touchpad on a Mac
- Pointers to pointers in C ++
- How to use pointer events in JavaScript
- How to create a yellow circle around the mouse cursor on Windows
- Cursor in C / C ++
- How to change mouse pointer on Google Chrome - Personalize mouse pointer