0% found this document useful (0 votes)
75 views

Pointer Arithmetics in C with Examples - GeeksforGeeks

Uploaded by

kvns.19810808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Pointer Arithmetics in C with Examples - GeeksforGeeks

Uploaded by

kvns.19810808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks

Pointer Arithmetics in C with Examples


Last Updated : 11 Oct, 2024

Pointer Arithmetic is the set of valid arithmetic operations that can be


performed on pointers. The pointer variables store the memory address of
another variable. It doesn’t store any value.

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

Decrement: It is a condition that also comes under subtraction. When a


pointer is decremented, it actually decrements 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 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.

To fully understand how to use pointers in complex data structures, the C


Programming Course Online with Data Structures provides practical
examples and in-depth explanations.

Example of Pointer Increment and Decrement


Courses @35% Off C C Basics C Data Types C Operators C Input and Output C Control Flow C Fun
Below is the program to illustrate pointer increment/decrement:
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 2/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks

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

Note: Pointers can be outputted using %p, since, most of the


computers store the address value in hexadecimal form using %p
gives the value in that form. But for simplicity and understanding we
can also use %u to get the value in Unsigned int form.

2. Addition of Integer to Pointer


When a pointer is added with an integer value, the value is first
multiplied by the size of the data type and then added to the pointer.

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

Example of Addition of Integer to Pointer

1 // C program to illustrate pointer Addition


2 #include <stdio.h>
3
4 // Driver Code
5 int main()
6 {
7 // Integer variable
8 int N = 4;
9
10 // Pointer to an integer
11 int *ptr1, *ptr2;
12
13 // Pointer stores the address of N
14 ptr1 = &N;
15 ptr2 = &N;
16
17 printf("Pointer ptr2 before Addition: ");
18 printf("%p \n", ptr2);
19
20 // Addition of 3 to ptr2

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

Pointer ptr2 before Addition: 0x7ffca373da9c


Pointer ptr2 after Addition: 0x7ffca373daa8

3. Subtraction of Integer to Pointer


When a pointer is subtracted with an integer value, the value is first
multiplied by the size of the data type and then subtracted from the
pointer similar to addition.

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.

Example of Subtraction of Integer from Pointer

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

Below is the program to illustrate pointer Subtraction:

1 // C program to illustrate pointer Subtraction


2 #include <stdio.h>
3
4 // Driver Code
5 int main()
6 {
7 // Integer variable
8 int N = 4;
9
10 // Pointer to an integer
11 int *ptr1, *ptr2;
12
13 // Pointer stores the address of N
14 ptr1 = &N;
15 ptr2 = &N;
16
17 printf("Pointer ptr2 before Subtraction: ");
18 printf("%p \n", ptr2);
19
20 // Subtraction of 3 to ptr2
21 ptr2 = ptr2 - 3;
22 printf("Pointer ptr2 after Subtraction: ");
23 printf("%p \n", ptr2);
24
25 return 0;
26 }

Output

Pointer ptr2 before Subtraction: 0x7ffd718ffebc


Pointer ptr2 after Subtraction: 0x7ffd718ffeb0

4. Subtraction of Two Pointers

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.

Example of Subtraction of Two Pointer

Below is the implementation to illustrate the Subtraction of Two Pointers:

1 // C program to illustrate Subtraction


2 // of two pointers
3 #include <stdio.h>
4
5 // Driver Code
6 int main()
7 {
8 int x = 6; // Integer variable declaration
9 int N = 4;
10
11 // Pointer declaration
12 int *ptr1, *ptr2;
13
14 ptr1 = &N; // stores address of N
15 ptr2 = &x; // stores address of x
16
17 printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);
18 // %p gives an hexa-decimal value,
19 // We convert it into an unsigned int value by using
%u

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

21 // Subtraction of ptr2 and ptr1


22 x = ptr1 - ptr2;
23
24 // Print x to get the Increment
25 // between ptr1 and ptr2
26 printf("Subtraction of ptr1 "
27 "& ptr2 is %d\n",
28 x);
29
30 return 0;
31 }

Output

ptr1 = 2715594428, ptr2 = 2715594424


Subtraction of ptr1 & ptr2 is 1

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.

Example of Pointer Comparision

1 // C Program to illustrare pointer comparision


2 #include <stdio.h>

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

Pointer to Array Name and First Element are Equal.

Comparison to NULL

A pointer can be compared or assigned a NULL value irrespective of what


is the pointer type. Such pointers are called NULL pointers and are used
in various pointer-related error-handling methods.

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

// C Program to demonstrate the pointer comparison with


NULL
2 // value
3 #include <stdio.h>
4
5 int main()
6 {
7
8 int* ptr = NULL;
9
10 if (ptr == NULL) {
11 printf("The pointer is NULL");
12 }
13 else {
14 printf("The pointer is not NULL");
15 }
16 return 0;
17 }

Output

The pointer is NULL

Comparison operators on Pointers using an array

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.

1. Step 1: First, declare the length of an array and array elements.


2. Step 2: Declare the pointer variable and point it to the first element of
an array.
3. Step 3: Initialize the count_even and count_odd. Iterate the for loop
and check the conditions for the number of odd elements and even
elements in an array
4. Step 4: Increment the pointer location ptr++ to the next element in an
array for further iteration.
5. Step 5: Print the result.
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 11/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks

Example of Pointer Comparison in Array

1 // Pointer Comparision in Array


2 #include <stdio.h>
3
4 int main()
5 {
6 int n = 10; // length of an array
7
8 int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
9 int* ptr; // Declaration of pointer variable
10
11 ptr = arr; // Pointer points the first (0th index)
12 // element in an array
13 int count_even = 0;
14 int count_odd = 0;
15
16 for (int i = 0; i < n; i++) {
17
18 if (*ptr % 2 == 0) {
19 count_even++;
20 }
21 if (*ptr % 2 != 0) {
22 count_odd++;
23 }
24 ptr++; // Pointing to the next element in an
array
25 }
26 printf("No of even elements in an array is : %d",
27 count_even);
28 printf("\nNo of odd elements in an array is : %d",
29 count_odd);
30 }

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

No of even elements in an array is : 5


No of odd elements in an array is : 5

Pointer Arithmetic on Arrays


Pointers contain addresses. Adding two addresses makes no sense
because there is no idea what it would point to. Subtracting two
addresses lets you compute the offset between the two addresses. An
array name acts like a pointer constant. The value of this pointer constant
is the address of the first element.

For Example: if an array is named arr then arr and &arr[0] can be used to
reference the array as a pointer.

Below is the program to illustrate the Pointer Arithmetic on arrays:

Program 1:

1 // C program to illustrate the array


2 // traversal using pointers
3 #include <stdio.h>
4
5 // Driver Code
6 int main()
7 {
8
9 int N = 5;
10
11 // An array
12 int arr[] = { 1, 2, 3, 4, 5 };
13
14 // Declare pointer variable
15 int* ptr;
16
17 // Point the pointer to first
18 // element in array arr[]
19 ptr = arr;
20

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

// Traverse array using ptr


22 for (int i = 0; i < N; i++) {
23
24 // Print element at which
25 // ptr points
26 printf("%d ", ptr[0]);
27 ptr++;
28 }
29 }

Output

1 2 3 4 5

Program 2:

1 // C program to illustrate the array


2 // traversal using pointers in 2D array
3 #include <stdio.h>
4
5 // Function to traverse 2D array
6 // using pointers
7 void traverseArr(int* arr, int N, int M)
8 {
9
10 int i, j;
11
12 // Traverse rows of 2D matrix
13 for (i = 0; i < N; i++) {
14
15 // Traverse columns of 2D matrix
16 for (j = 0; j < M; j++) {
17
18 // Print the element
19 printf("%d ", *((arr + i * M) + j));
20 }
21 printf("\n");
https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 14/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks

}
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!

Comment More info Next Article


Applications of Pointers in C

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

Difference between Dangling pointer and Void pointer


Dangling pointer: A pointer pointing to a memory location that has been
deleted (or freed) is called a dangling pointer. There are three different…
2 min read

Pointer to an Array | Array Pointer


A pointer to an array is a pointer that points to the whole array instead of
the first element of the array. It considers the whole array as a single unit…
5 min read

How to declare a pointer to a function?


While a pointer to a variable or an object is used to access them indirectly, a
pointer to a function is used to invoke a function indirectly. Well, we assum…
2 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

C | Pointer Basics | Question 1


What is the output of following program? # include <stdio.h> void fun(int x)
{ x = 30; } int main() { int y = 20; fun(y); printf("%d", y); return 0; } (A) 30 (B)…
1 min read

C | Pointer Basics | Question 2


https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 16/20
12/10/24, 5:42 AM Pointer Arithmetics in C with Examples - GeeksforGeeks

Output of following program? # include <stdio.h> void fun(int *ptr) { *ptr =


30; } int main() { int y = 20; fun(&y); printf("%d", y); return 0; } (A) 20 (B) 30…
1 min read

C | Pointer Basics | Question 3


Output of following program? #include <stdio.h> int main() { int *ptr; int x;
ptr = &x; *ptr = 0; printf(" x = %d\n", x); printf(" *ptr = %d\n", *ptr); *ptr += …
2 min read

C | Pointer Basics | Question 4


Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer
takes 4 bytes. #include <stdio.h> int main() { int arri[] = {1, 2 ,3}; int *ptri =…
1 min read

C | Pointer Basics | Question 17


Assume that float takes 4 bytes, predict the output of following program.
#include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};…
1 min read

Article Tags : C Language C-Advanced Pointer C-Pointer Basics C-Pointers +1 More

Practice Tags : Pointers

Corporate & Communications Address:-


A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305) | Registered Address:- K 061,
Tower K, Gulshan Vivante Apartment,
Sector 137, Noida, Gautam Buddh
Nagar, Uttar Pradesh, 201305

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

Data Science & ML Web Technologies


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning JavaScript
ML Maths TypeScript
Data Visualisation ReactJS
Pandas NextJS
NumPy NodeJs
NLP Bootstrap
Deep Learning Tailwind CSS

Python Tutorial Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design

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

Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

School Subjects Commerce


Mathematics Accountancy
Physics Business Studies
Chemistry Economics
Biology Management
Social Science HR Management
English Grammar Finance
Income Tax

Databases Preparation Corner


SQL Company-Wise Recruitment Process
MYSQL Resume Templates
PostgreSQL Aptitude Preparation
PL/SQL Puzzles
MongoDB Company-Wise Preparation
Companies
Colleges

Competitive Exams More Tutorials


JEE Advanced Software Development
UGC NET Software Testing
UPSC Product Management
SSC CGL Project Management
SBI PO Linux
SBI Clerk Excel
IBPS PO All Cheat Sheets
IBPS Clerk Recent Articles

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships

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

Random Number Generator


Random Password Generator

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]

Machine Learning/Data Science Programming Languages


Complete Machine Learning & Data Science Program - [LIVE] C Programming with Data Structures
Data Analytics Training using Excel, SQL, Python & PowerBI - C++ Programming Course
[LIVE] Java Programming Course
Data Science Training Program - [LIVE] Python Full Course
Mastering Generative AI and ChatGPT

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

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/ 20/20

You might also like