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 should read it
- How to Change Your Cursor
- Macromedia Flash - Customize cursor
- 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
- Macromedia Flash - Dynamic cursor object
- Macromedia Flash - Move objects
Maybe you are interested
Manifest V3 rollout to remove Google extensions is being pushed Use Vitamins Correctly How to customize the clock in the Windows system tray 10 strange and interesting facts about the universe you may not have heard of The burial chamber of the deceased due to Covid-19 in Iran is large enough to be seen from space New technology helps NASA find water, self-sufficient food on the moon