Pointer Arithmetic in C++ with Programming Examples

(Last Updated On: April 2, 2021)

Pointer Arithmetic in C++:

pointer arithmetic in C++:- We can perform two arithmetic operation on pointers. These are addition and subtraction operations. A pointer arithmetic in C++ may be incremented or decremented. It means that we can add or subtract integer value to and from the pointer. Similarly, a pointer arithmetic can be subtracted( or added) from another.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

The addition and subtraction operation on pointers are different than that of ordinary arithmetic operations. Following set of statements explains the pointer arithmetic in C++:

Int *p;

Int x;

P=&x;

P++;

Suppose the memory address allocated to variable ‘x’ is 200 which is assigned to ‘p’. Both ‘p’ and ‘x’ are of  int type. The size of int type is 2 bytes. When the statement p++ is executed, the contents of p will be 202 (instead of 201). When an integer is added or subtracted from a pointer, the pointer in not incremented or decremented by that integer but is incremented or decremented by integer times the size of the object to which the pointer refers. The size of the object depends upon its data type. So the statement p++ will be evaluated as:

200+1*2=202

pointer arithmetic in C++

Each time a pointer is incremented, it points to the memory location of the next element of its base type. Similarly, each time it is decremented, it points to the location of the previous element. In case of pointers to characters, the increment and decrement operations are performed as normal.


However, all the other pointer variable are incremented or decremented by the size of data type they point to. For example, pointer variable ‘pp’ that points to ‘float’ data type, holds the memory address 4000 and value 2 is added to it as:

pp=pp+2;

after execution the above statement, the value 4008(4000+2*4 = 4008) will be assigned to pp.

pointer arithmetic in C++

A pointer can be assigned to another one if both the pointers are of the same type. Otherwise a cast operator must be used to convert the value of the pointer on the right side of the assignment operator (=) to the pointer type on the left side of the assignment operator.



Programming Examples:

Write a program that initializes integer values to an array and displays the value of the array on the screen using pointer notation and pointer arithmetic in C++:

pointer arithmetic in C++

Write a program that inputs values into an array and displays the odd value of array on screen using pointer notation and pointer arithmetic in C++:

pointer arithmetic in C++


Write a program that inputs value into array and display in reverse order on screen using pointer notation and pointer arithmetic in C++:

pointer arithmetic in C++

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: