In this article Pointer Arithmetic in C we give the information about when we manipulate the address stored in a pointer using arithmetic operators, the process is known as Pointer Arithmetic.
Pointer Arithmetic in C
In C programming, a pointer is a variable that stores the address of another variable. Since a pointer also has its own address, we can perform arithmetic operations on it. When we manipulate the address stored in a pointer using arithmetic operators, the process is known as Pointer Arithmetic.
Pointer arithmetic differs from normal arithmetic because the compiler takes the data type size into account when performing operations on pointers.
Types of Pointer Arithmetic Operations
C language allows the following arithmetic operations on pointers:
- Increment (++)
- Decrement (–)
- Addition (+)
- Subtraction (-)
- Comparison (<, <=, >, >=, ==, !=)
1) Increment (++)
When we use the increment operator on a pointer, it increases the pointer’s address by the size of its data type.
For example, on a 32-bit system:
- An int occupies 4 bytes.
- A char occupies 1 byte.
- A double occupies 8 bytes.
Hence, incrementing an int* increases its value by 4, while incrementing a char* increases it by 1.
Example:
#include<stdio.h>
int main() {
int i = 12, *ip = &i;
double d = 2.3, *dp = &d;
char ch = ‘a’, *cp = &ch;
printf(“Address of ip = %u\n”, ip);
printf(“Address of dp = %u\n”, dp);
printf(“Address of cp = %u\n\n”, cp);
printf(“After ip + 1 = %u\n”, ip + 1);
printf(“After dp + 1 = %u\n”, dp + 1);
printf(“After cp + 1 = %u\n”, cp + 1);
return 0;
}
Output (example):
Address of ip = 53316
Address of dp = 53304
Address of cp = 53303
After ip + 1 = 53320
After dp + 1 = 53312
After cp + 1 = 53304
2) Decrement (–)
When we decrement a pointer, it decreases the pointer’s address by the size of its data type.
Example:
- For an int* (4 bytes), if the pointer address is 1000, then –ip makes it 996 (1000 – 4).
- For a char* (1 byte), if the address is 3000, then –cp makes it 2999 (3000 – 1).
3) Addition (+)
We can add an integer value to a pointer. The new address is calculated by adding the integer multiplied by the size of the data type.
Formula:
new_address = current_address + (n * sizeof(data_type))
Example:
#include<stdio.h>
int main() {
int num = 15;
int *p = #
printf(“Address of p = %u\n”, p);
p = p + 2;
printf(“After adding 2: Address of p = %u\n”, p);
return 0;
}
Output (example):
Address of p = 34302
After adding 2: Address of p = 34310
4) Subtraction (-)
We can subtract an integer from a pointer. The pointer moves backward by a number of elements equal to the value multiplied by the data type size.
Formula:
new_address = current_address – (n * sizeof(data_type))
Example:
#include<stdio.h>
int main() {
int num = 15;
int *p = #
printf(“Address of p = %u\n”, p);
p = p – 2;
printf(“After subtracting 2: Address of p = %u\n”, p);
return 0;
}
Output (example):
Address of p = 34310
After subtracting 2: Address of p = 34302
5) Comparison
Pointers can be compared using relational operators (==, !=, <, >, <=, >=).
However, comparison is meaningful only if both pointers point to elements of the same array or object.
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr1 = #
int *ptr2 = #
if (ptr1 == ptr2)
printf(“Both pointers point to the same memory location”);
return 0;
}
Output:
Both pointers point to the same memory location
Subtraction Between Two Pointers
Subtraction between two pointers is valid only if both are of the same data type.
The result gives the number of elements between them, not the byte difference.
Example:
#include <stdio.h>
int main() {
int N = 4;
int *ptr1 = &N;
int *ptr2 = &N;
ptr2 = ptr2 + 3;
int x = ptr2 – ptr1;
printf(“Subtraction of ptr1 & ptr2 is %d\n”, x);
return 0;
}
Output:
Subtraction of ptr1 & ptr2 is 3
Rules for Pointer Arithmetic
- Addition, subtraction, and comparison are valid operations on pointers.
- Multiplication and division on pointers are not allowed.
- Pointers of different data types should not be subtracted or compared.
- Adding or subtracting an integer value to a pointer gives a new valid memory address.
- Pointer arithmetic is mostly used with arrays, since arrays store elements in contiguous memory locations.
Key Points Summary
- Pointer arithmetic adjusts addresses based on the data type size.
- Increment and decrement move the pointer to the next or previous memory location.
- Addition and subtraction shift the pointer by multiple elements.
- Pointer comparison checks if two pointers refer to the same or relative memory positions.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining