Pointer Arithmetics in C
Pointer Arithmetics in C
Pointer Arithmetics in C
Pointer Arithmetics in C
A pointer variable in C stores the address of another variable. The address is always
an integer. So, can we perform arithmetic operations such as addition and
subtraction on the pointers? In this chapter, we will explain which arithmetic
operators use pointers in C as operands, and which operations are not defined to be
performed with pointers.
Let us discuss all these pointer arithmetic operations in detail with the help of
examples.
Assume that an integer variable "x" is created at address 1000 in the memory, with
10 as its value. Then, "x++" makes the value of "x" as 11.
x++; // x becomes 11
What happens if we declare "y" as pointer to "x" and increment "y" by 1 (with
"y++")? Assume that the address of "y" itself is 2000.
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 1/8
6/16/24, 12:50 PM Pointer Arithmetics in C
Since the variable "y" stores 1000 (the address of "x"), we expect it to become 1001
because of the "++" operator, but it increments by 4, which is the size of "int"
variable.
The is why because, if the address of "x" is 1000, then it occupies 4 bytes: 1000,
1001, 1002 and 1003. Hence, the next integer can be put only in 1004 and not
before it. Hence "y" (the pointer to "x") becomes 1004 when incremented.
#include <stdio.h>
int main(){
int x = 10;
int *y = &x;
y++;
Output
You can see that the value has increased by 4. Similarly, the "--" operator
decrements the value by the size of the data type.
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 2/8
6/16/24, 12:50 PM Pointer Arithmetics in C
Let us change the types of "x" and "y" to "double" and "float" and see the effect of
decrement operator.
#include <stdio.h>
int main(){
double x = 10;
double *y = &x;
y--;
Output
When an array is declared, the elements are stored in adjacent memory locations. In
case of "int" array, each array subscript is placed apart by 4 bytes, as the following
figure shows −
Hence, if a variable stores the address of 0th element of the array, then the
"increment" takes it to the 1st element.
The following example shows how you can traverse an array by incrementing a
pointer successively −
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 3/8
6/16/24, 12:50 PM Pointer Arithmetics in C
int main(){
int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int len = sizeof(a)/sizeof(int);
int *x = a;
int i = 0;
return 0;
}
Output
Addition and subtraction of an integer to a pointer does not add and subtract that
value to the pointer, multiplication with the size of the data type is added or
subtracted to the pointer.
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 4/8
6/16/24, 12:50 PM Pointer Arithmetics in C
For example, there is an integer pointer variable ptr and it is pointing to an address
123400, if you add 1 to the ptr (ptr+1), it will point to the address 123404 (size of
an integer is 4).
ptr = 123400
ptr = ptr + 1
ptr = ptr + sizeof(int)*1
ptr = 123400 + 4
ptr = 123404
#include <stdio.h>
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = int_arr;
// Adding 2 in ptrArr
ptrArr = ptrArr + 2;
return 0;
}
Output
Value at ptrArr: 12
Value at ptrArr after adding 2: 45
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 5/8
6/16/24, 12:50 PM Pointer Arithmetics in C
#include <stdio.h>
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = &int_arr[4]; // points to last element
// Subtracting 2 in ptrArr
ptrArr = ptrArr - 2;
return 0;
}
Output
Value at ptrArr: 89
Value at ptrArr after adding 2: 45
Subtraction of Pointers
We are familiar with the "+" and "−" operators when they are used with regular
numeric operands. However, when you use these operators with pointers, they
behave in a little different way.
Since pointers are fairly large integers (especially in modern 64-bit systems),
addition of two pointers is meaningless. When we add a 1 to a pointer, it points to
the next location where an integer may be stored. Obviously, when we add a pointer
(itself a large integer), the location it points may not be in the memory layout.
However, subtraction of two pointers is realistic. It returns the number of data types
that can fit in the two pointers.
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 6/8
6/16/24, 12:50 PM Pointer Arithmetics in C
Let us take the array in the previous example and perform the subtraction of
pointers of a[0] and a[9]
#include <stdio.h>
int main(){
int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int *x = &a[0]; // zeroth element
int *y = &a[9]; // last element
Output
It can be seen that the numerical difference between the two integers is 36; it
suggests that the subtraction is 9, because it can accommodate 9 integers between
the two pointers.
Comparison of Pointers
Pointers may be compared by using relational operators such as "==", "<", and ">".
If "p1" and "p2" point to variables that are related to each other (such as elements
of the same array), then "p1" and "p2" can be meaningfully compared.
In the following example, we are declaring two pointers and initializing them with the
first and last elements of the array respectively. We will keep incrementing the first
variable pointer as long as the address to which it points is either less than or equal
to the address of the last element of the array, which is "&var[MAX − 1]" (i.e., the
second pointer).
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 7/8
6/16/24, 12:50 PM Pointer Arithmetics in C
#include <stdio.h>
int main() {
int var[] = {10, 100, 200};
int i, *ptr1, *ptr2;
// Initializing pointers
ptr1 = var;
ptr2 = &var[MAX - 1];
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm 8/8