Pointer Arithmetics in C with Examples - GeeksforGeeks
Pointer Arithmetics in C with Examples - GeeksforGeeks
Hence, there are only a few operations that are allowed to perform on
Pointers in C language. The C pointer arithmetic operations are slightly
different from the ones that we generally use for mathematical
calculations. These operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
5. Comparison of pointers
1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a
pointer is incremented, it actually increments by the number equal to the
size of the data type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int), and the new address will point to 1004.
While if a float type pointer is incremented then it will increment by
4(size of a float) and the new address will be 1004.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 1/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
For Example:
If an integer pointer that stores address 1000 is decremented, then it will
decrement by 4(size of an int), and the new address will point to 996.
While if a float type pointer is decremented then it will decrement by
4(size of a float) and the new address will be 996.
Note: It is assumed here that the architecture is 64-bit and all the
data types are sized accordingly. For example, integer is of 4 bytes.
1 #include <stdio.h>
2 // pointer increment and decrement
3 //pointers are incremented and decremented by the
size of the data type they point to
4 int main()
5 {
6 int a = 22;
7 int *p = &a;
8 printf("p = %u\n", p); // p = 6422288
9 p++;
10 printf("p++ = %u\n", p); //p++ = 6422292 +4
// 4 bytes
11 p--;
12 printf("p-- = %u\n", p); //p-- = 6422288 -4
// restored to original value
13
14 float b = 22.22;
15 float *q = &b;
16 printf("q = %u\n", q); //q = 6422284
17 q++;
18 printf("q++ = %u\n", q); //q++ = 6422288 +4
// 4 bytes
19 q--;
20 printf("q-- = %u\n", q); //q-- = 6422284 -4
// restored to original value
21
22 char c = 'a';
23 char *r = &c;
24 printf("r = %u\n", r); //r = 6422283
25 r++;
26 printf("r++ = %u\n", r); //r++ = 6422284 +1
// 1 byte
27 r--;
28 printf("r-- = %u\n", r); //r-- = 6422283 -1
// restored to original value
29
30 return 0;
31 }
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 3/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Output
p = 1441900792
p++ = 1441900796
p-- = 1441900792
q = 1441900796
q++ = 1441900800
q-- = 1441900796
r = 1441900791
r++ = 1441900792
r-- = 1441900791
For Example:
Consider the same example as above where the ptr is an integer pointer
that stores 1000 as an address. If we add integer 5 to it using the
expression, ptr = ptr + 5, then, the final address stored in the ptr will be
ptr = 1000 + sizeof(int) * 5 = 1020.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 4/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 5/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
ptr2 = ptr2 + 3;
22 printf("Pointer ptr2 after Addition: ");
23 printf("%p \n", ptr2);
24
25 return 0;
26 }
Output
For Example:
Consider the same example as above where the ptr is an integer pointer
that stores 1000 as an address. If we subtract integer 5 from it using the
expression, ptr = ptr – 5, then, the final address stored in the ptr will be
ptr = 1000 – sizeof(int) * 5 = 980.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 6/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Output
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 7/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
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 and calculating how many bits of data
it is according to the pointer data type. The subtraction of two pointers
gives the increments between the two pointers.
For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are
subtracted. The difference between addresses is 4 bytes. Since the size of
int is 4 bytes, therefore the increment between ptr1 and ptr2 is given by
(4/4) = 1.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 8/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Output
5. Comparison of Pointers
We can compare the two pointers by using the comparison operators in
C. We can implement this by using all operators in C >, >=, <, <=, ==, !=.
It returns true for the valid condition and returns false for the unsatisfied
condition.
1. Step 1: Initialize the integer values and point these integer values to
the pointer.
2. Step 2: Now, check the condition by using comparison or relational
operators on pointer variables.
3. Step 3: Display the output.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 9/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
4 int main()
5 {
6 // declaring array
7 int arr[5];
8
9 // declaring pointer to array name
10 int* ptr1 = &arr;
11 // declaring pointer to first element
12 int* ptr2 = &arr[0];
13
14 if (ptr1 == ptr2) {
15 printf("Pointer to Array Name and First Element
"
16 "are Equal.");
17 }
18 else {
19 printf("Pointer to Array Name and First Element
"
20 "are not Equal.");
21 }
22
23 return 0;
24 }
Output
Comparison to NULL
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 10/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Output
In the below approach, it results in the count of odd numbers and even
numbers in an array. We are going to implement this by using a pointer.
Output
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 12/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
For Example: if an array is named arr then arr and &arr[0] can be used to
reference the array as a pointer.
Program 1:
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 13/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Output
1 2 3 4 5
Program 2:
}
23 }
24
25 // Driver Code
26 int main()
27 {
28
29 int N = 3, M = 2;
30
31 // A 2D array
32 int arr[][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
33
34 // Function Call
35 traverseArr((int*)arr, N, M);
36 return 0;
37 }
Output
1 2
3 4
5 6
Subscribe for 1 Year and get 1 Extra year of access completely FREE!
Upgrade to GeeksforGeeks Premium today!
Choose GeeksforGeeks Premium and also get access to 50+ Courses with
Certifications, Unlimited Article Summarization, 100% Ad free
environment, A.I. Bot support in all coding problems, and much more. Go
Premium!
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 15/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Similar Reads
C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we
want to store the address of another pointer. The first pointer is used to…
5 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the
same, the major exceptions being: 1. the sizeof operator sizeof(array) retur…
1 min read
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 17/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 18/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 19/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks
DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
Core CS Subject for Interview Preparation JAVA Backend Development - [LIVE]
Mastering System Design: LLD to HLD Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]
Clouds/Devops GATE
DevOps Engineering GATE CS & IT Test Series - 2025
AWS Solutions Architect Certification GATE DA Test Series 2025
Salesforce Certified Administrator Course GATE CS & IT Course - 2025
GATE DA Course 2025
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 20/20