Site icon Learn C++

Learn To Use Arithmetics On Pointers In C++

In a previous post, we learned that pointers are variables to point address which is also an integer value. That means we can do arithmetic operations on a pointer variable. But do not forget that we operate on the addresses in our RAMs, so we should be careful and we should know the range well. Simply, we can use ++, –, +, and – arithmetic operators on pointers:

In a 32bit application, the pointer addresses 32bit. So the increment of pointer, when you increase the address 32bits, means that you increase that pointer to 4 bytes forward. If you decrease that pointer, it also means you decrease the address to 4 bytes backward. In a 64bits application, this means 64bits change that results in 8bits forward or backward address.

We can easily understand all these in this example below,

[crayon-6742d1b893c36103094853/]

Comparison in Pointers

Pointers are integer values that points address in memory, they can be also compared as integers by using relational operators such as ‘==’, ‘<‘, ‘>’. We can check two pointers if they are pointing the same address, see example below.

[crayon-6742d1b893c40923758362/]

‘<‘ and ‘>’ comparison operators can be used in array pointers as given above.

Exit mobile version