# Pointer Arithmetic in C language

“You Don’t Have To Be Great To Start , But You Have To Start To Be Great”

Hello everyone!!!

Hope you all must be doing well……..:)

Today we are going to discuss Pointer Arithmetic in C language.

Soo let’s start:)

Pointer in C is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic operators that can be used on pointers: ++, –, +and -.

Let us study all these operators one by one in detail:-

1) Incrementing a Pointer:-
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer –
Ptr++
• After the above operation ie:- increment , the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location.
• This operation will move the pointer to the next memory location without impacting the actual value at the memory location.
• If ptr points to a character whose address is 1000, then the above operation will point to the location 1001 because the next character will be available at 1001.
• We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.

Example:-
Use of increment operator to point to next memory location

Example 2:-
Display Array Elements using Pointer.

2)Decrementing a Pointer:-
•The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type.
• Depending on the data type of the variable declared it decreases the value by that much byte.

•Example:-
Use of decrement operator to point to previous location.

Example 2:-
Display Reverse of Array using Pointer.

We can add a value to the pointer variable. The formula of adding value to pointer is given below:

Let us try to understand with the help of an example below:

Explanation:-
Here the statement p=p+5 will add 5*no of bytes required for storing integer variable. For eg:- if p contains 1000 and integer takes 2 bytes for storage then we will get. p=1000 + 5*2 = 1010

4) C Pointer Subtraction:-
Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address. The formula of subtracting value from the pointer variable is given below:

Example on pointer subtraction:

Explanation:-
Here the statement p=p-3 will subtract 3*no of bytes required for storing
integer variable. For eg:- if p contains 1000 and integer takes 2 bytes for storage
then we will get. p=1000 – 3*2 =994

5) Subtraction of Two Pointers:-
•The subtraction of two pointers is possible only when they have the same data type.
• The result is generated by calculating the difference between the addresses of the two pointers.
• The subtraction of two pointers gives the increments between the two pointers.ie:-
It gives by how many bytes the pointer is increment.

Example:-

Here the difference printed will be in number of bytes.

Soo this is all about pointer arithmetic.

We will discuss some more related topics in upcoming articles.

Thank you so much all for your support 🙂

Have a wonderful day ahead 🙂