Pointer Arithmetic In C [Explained With Examples] – CsTutorialpoint
Hello Friends, In today’s article we are going to talk about Pointer Arithmetic Operations In C Language.
Today we will learn in detail about, what is Pointer Arithmetic In C and how Pointer Arithmetic is used in C language.
So now without wasting any time let’s understand about Pointer Arithmetic In C
Pointer Arithmetic In C
As we know, a pointer is a variable that contains the address of another variable and since a pointer is also a variable, it also has an address. We can manipulate this address by arithmetic operation (addition, subtraction etc.) and when we do this, then it is called Pointer Arithmetic.
Pointer arithmetic is a little different from normal arithmetic (which we generally use in our daily life).
C language provides us some set of operators for Pointer Arithmetic operations which is as follows -:
- Increment
- Decrement
- Addition
- Subtraction
- Comparison
Let’s now understand one by one about all these Pointer arithmetic operators
1) Increment
When we use increment operator (++) with pointer variable then it returns the next address. This next address is equal to the sum of the current pointer address and the size of the pointer (according to the 32-bit system the size of the pointer is 4 bytes).
For example -: If we have an integer pointer ip whose address is 1000, then increasing it by 1 we will get 1004 (ie 1000 + 1 * 4) instead of 1001 because the size of int data type is 4 bytes. But if we are using a system where size of int is 2 bytes then we will get 1002 (ie 1000 + 1 * 2) which will be next memory address.
Let’s understand these things with an example.
Example -:
int i = 12, *ip = &i;
double d = 2.3, *dp = &d;
char ch = 'a', *cp = &ch;
Let the addresses of i, d, and ch be 1000, 2000, 3000 respectively.
Pointer Arithmetic on Integers
Pointer Expression | How it is evaluated ? |
ip = ip + 1 | ip => ip + 1 => 1000 + 1*4 => 1004 |
ip++ or ++ip | ip++ => ip + 1 => 1004 + 1*4 => 1008 |
Pointer Arithmetic on Float
Pointer Expression | How it is evaluated ? |
dp + 1 | dp = dp + 1 => 2000 + 1*8 => 2008 |
dp++ or ++dp | dp++ => dp+1 => 2008+1*8 => 2016 |
Pointer Arithmetic on Characters
Pointer Expression | How it is evaluated ? |
cp + 1 | cp = cp + 1 => 3000 + 1*1 => 3001 |
cp++ or ++cp | cp => cp + 1 => 3001 + 1*1 => 3002 |
Note -:
- By performing an arithmetic operation on a pointer to a char type, it looks like a normal arithmetic operation because the size of the char data type is 1 byte.
- Another important thing to note is that when we increase and decrease the pointer variable by adding or subtracting numbers then it is not necessary that the pointer variable should point to a valid memory location. Therefore, we should always pay special attention when we move the pointer in this way.
- Generally, we use pointer arithmetic with arrays because array elements are arranged in a contiguous fashion.
Example Program
#include<stdio.h>
void main()
{
int i = 23, *ip = &i;
double d = 3.2, *dp = &d;
char ch = 'a', *cp = &ch;
printf("Value of ip = %u\n", ip);
printf("Value of dp = %u\n", dp);
printf("Value of cp = %u\n\n", cp);
printf("Value of ip + 1 = %u\n", ip + 1);
printf("Value of dp + 1 = %u\n", dp + 1);
printf("Value of cp + 1 = %u\n\n", cp + 1);
}
Output -:
Value of ip = 2293316
Value of dp = 2293304
Value of cp = 2293303
Value of ip + 1 = 2293320
Value of dp + 1 = 2293312
Value of cp + 1 = 2293304
Note -: Here the numbers (like 2293316, 2293304, 2293303) that have come in the output are memory addresses. These addresses may also differ depending on your system.
2) Decrement (–)
When we use decrement operator (–) with pointer variable then it returns the previous address. This last address is equal to the subtraction of the current pointer address and the size of the pointer (according to the 32 bit system the size of the pointer is 4 bytes).
For example -: If we have an integer pointer ip whose address is 1000, then subtracting it from 1 we will get 996 (ie 1000 – 1 * 4) instead of 999 because the size of int data type is 4 bytes. But if we are using a system where size of int is 2 bytes then we will get 998 (ie 1000 – 1 * 2).
Let’s understand these things with an example.
Example -:
int i = 12, *ip = &i;
double d = 2.3, *dp = &d;
char ch = 'a', *cp = &ch;
Let the addresses of i, d and ch be 1000, 2000, 3000 respectively.
Pointer Expression | How it is evaluated? |
ip– or –ip | ip => ip – 1 => 1000 – 1*4 => 996 |
dp– or –dp | dp => dp – 1=>2000 – 1*8=>1992` |
cp– or –cp | cp => cp – 1 => 3000 – 1*1 => 2999 |
Let’s now understand about the addition operator in Pointer Arithmetic.
Addition
When we add a value to a pointer, it is first multiplied by a value equal to the pointer size, then it is added to the pointer, from this we get the next pointer address.
Syntax
new_address = current_address + (number * size_of(data type))
Example program
#include<stdio.h>
int main()
{
int num = 5;
int *p;
//stores the address of num variable
p=#
printf("Address of p variable is %u \n",p);
//adding 3 to pointer variable
p=p+3;
printf("After adding 3: Address of p variable is %u \n",p);
return 0;
}
Output -:
Address of p variable is 34864302
After adding 3: Address of p variable is 34864314
Note -:
- This output may be different in your computer system
- If your system is 16 bit then here int data type will consume 2 byte memory and if your system is 32 bit or 64 bit then int data type will consume 4 byte memory.
3) Subtraction
Just as we can perform addition operation with pointer, in the same way we can also perform subtraction operation with pointer. When we subtract any value in the pointer, it returns an address which is just the previous address of that pointer. Its syntax is something like this –
Syntax
new_address= current_address - (number * size_of(data type))
Example program
#include<stdio.h>
int main()
{
int num = 5;
int *p;
//stores the address of number variable
p=#
printf("Address of p variable is %u \n",p);
//Subtracting 3 to pointer variable
p=p-3;
printf("After Subtracting 3: Address of p variable is %u \n",p);
return 0;
}
Output -:
Address of p variable is 34864314
After Subtracting 3: Address of p variable is 34864302
4) Comparison
- We can use relational operators ( <, <=, >, >= , == , !=) with pointers.
- The == and != operators are used to compare two pointers.
- The use of other relational operators (<, <=, >, >=) to compare two pointers is meaningful only if they both point to the same array elements.
- Pointer comparisons ( <, <=, >, >= , == , !=) are not used much compared to pointer arithmetic (addition, subtraction).
- comparisons are useful if you want to check whether two pointers are pointing to the same location.
Example Program
#include <stdio.h>
int main()
{
int num = 10;
int *ptr1 = # // ptr1 points to num
int *ptr2 = # // ptr2 also points to num
if(ptr1 == ptr2)
{
// Do some task
printf("Both pointers points to same memory location");
}
return 0;
}
Output
Both pointers points to same memory location
Subtraction of Two Pointers
Subtraction between two pointers is possible only if they are both of the same data type.
Example Program
// C program to illustrate Subtraction of two pointers
#include <stdio.h>
int main()
{
int x;
// Integer variable
int N = 4;
// Pointer to an integer
int *ptr1, *ptr2;
// Pointer stores the address of N
ptr1 = &N;
ptr2 = &N;
// Incrementing ptr2 by 3
ptr2 = ptr2 + 3;
// Subtraction of ptr2 and ptr1
x = ptr2 - ptr1;
// Print x to get the Increment between ptr1 and ptr2
printf("Subtraction of ptr1 & ptr2 is %d\n", x);
return 0;
}
Output
Subtraction of ptr1 & ptr2 is 3
Rules for Performing Pointer Arithmetic
There are many operations that cannot be performed on pointers. Since the pointer stores the address, we should ignore those operations which can provide illegal addresses.
Below I have told about some legal or illegal pointer arithmetic operations.
- We cannot perform addition, multiplication, and divisional operation between two addresses, but we can do subtraction.
- We can subtract addresses of the same type.
- We cannot multiply and divide an integer value with an address, but we can add and subtract integer values to an address.
- The value obtained by adding or subtracting an integer value to an address is also an address.
- Address + Address = illegal
- Address * Address = illegal
- Address % Address = illegal
- Address / Address = illegal
- Address & Address = illegal
- Address ^ Address = illegal
- Address | Address = illegal
- ~Address = illegal
So, friends, these were some rules related to performing pointer arithmetic operations, which you always have to keep in mind.
Well, below I have given a video which you must watch once. In this video, Pointer Arithmetic has been explained very well.
Read More
- Introduction of Pointer in C
- Address Operator (&) In C
- Indirection Operator(*) In C
- Pointer to Pointer in C
- Pointer to Structure In C
- Dangling Pointer in C
- Wild Pointer In C
- Null Pointer In C
- Void Pointer In C
- Dynamic Memory Allocation in C Programming
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is pointer Arithmetic In C Language and how to use pointer Arithmetic in C language
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about pointer Arithmetic In C
To get more information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.