C++Introduction to C++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,

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.

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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

C++Introduction to C++Learn C++

Learn Classes and Objects in C++

C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes

Worth reading...
Learn to Use Pointers and Memory Address of a Variable in C++
en_USEnglish