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 -.
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 should read it
- Instructions to change the interface of the mouse cursor on Windows
- How to Create and Apply a Custom Mouse Cursor Using a Photo in Windows
- Steps to customize the cursor with Custom Cursor in Google Chrome
- Cursor in C
- Safe code in C #
- How to change the mouse cursor in Windows
- Replace the mouse cursor with funny pictures
- How to change the cursor size on the Debian 10 desktop
May be interested
- How to change the pointer sensitivity, touchpad on a Maccursor is the primary, indispensable means that you need to interact with the screen of your mac. whether the pointer speed is too slow or too fast can ruin your experience, as well as severely affect work efficiency.
- Pointers to pointers in C ++a pointer to a cursor is an unidirected form or a string of pointers. typically, a pointer contains the address of a variable.
- How to use pointer events in JavaScriptjavascript has a newer standard called pointer events. it handles both mouse and touch operations, so you don't have to worry about implementing each separately.
- How to create a yellow circle around the mouse cursor on Windowstypically, most users want to add a yellow circle behind the mouse cursor to highlight it. in this quick post, tipsmake.com will show you how to create a yellow circle around the mouse pointer on windows 10.
- Cursor in C / C ++pointer - pointer in c / c ++ language is easy to learn and interesting. some tasks in the c / c ++ language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a pointer.
- How to change mouse pointer on Google Chrome - Personalize mouse pointerchanging the mouse pointer on google chrome is very simple with some mouse pointer changing utilities on the browser. utilities will change the mouse pointer on chrome in many different ways
- The basic steps of fixing cursor cursors flutter in Windowsthe status of the mouse pointer jumps, the mouse is disordered or the mouse moves automatically while typing the keyboard occurs quite popular, it causes inhibition for the user to keep the mouse to hold the cursor back. this may be because the touchpad on your laptop is too sensitive or maybe because you accidentally touched the touchpad when typing. please try to apply the following corrections below.
- 3 ways to quickly change the mouse pointerin the following article, tipsmake will guide you through 3 quick and effective ways to change the mouse pointer.
- Cursor to class in C ++a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.
- Instructions to customize the mouse pointer in Windowsif you feel that the usual white windows mouse pointer is too boring or difficult to see, everyone can completely customize it according to their preferences with the available settings.