0% found this document useful (0 votes)
61 views21 pages

Pointer Arithmetic in C++ Explained

The document provides an overview of pointer arithmetic in C++, explaining how pointers store addresses and can be manipulated using arithmetic operations. It covers incrementing, decrementing, adding, and subtracting integers from pointers, as well as comparing pointers. Examples are included to illustrate these concepts in practice.

Uploaded by

dubeyanushka131
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)
61 views21 pages

Pointer Arithmetic in C++ Explained

The document provides an overview of pointer arithmetic in C++, explaining how pointers store addresses and can be manipulated using arithmetic operations. It covers incrementing, decrementing, adding, and subtracting integers from pointers, as well as comparing pointers. Examples are included to illustrate these concepts in practice.

Uploaded by

dubeyanushka131
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

1

SDF II(15B11CI211)
EVEN Semester 2024

2nd Semester , First Year


Jaypee Institute Of Information Technology (JIIT), Noida
2

Lecture 4 – Pointers arithmetic in C++


Pointers 3
✔ Pointers store address of variables or a memory location.
✔ // General syntax
datatype *var_name;

// An example pointer "ptr" that holds address of an integer variable or holds address of a memory whose
value(s) can be accessed as integer values through "ptr"
int *ptr;
POINTER ARITHMETIC: 4

• pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a
pointer just as you can a numeric value.
• There are four arithmetic operators that can be used on pointers:
1. Increment Operator ++,
2. Decrement Operator --,
3. Addition ,
4. Subtraction
5. Comparison (>, >=, <, <=, ==)
POINTER ARITHMETIC: 5
• When you increment a pointer, it moves to the next memory location based on the size of the data it points
to.

• To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address
1000.
• Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer—
ptr++
• The ptr will point to the location 1004 because each time ptr is incremented, it will point to the next
integer.
• This operation will move the pointer to next memory location without impacting actual value at the
memory location.
Incrementing a Pointer 6
• We prefer using a pointer in our program instead of an array because the variable pointer can be
incremented, unlike the array name which cannot be incremented because it is a constant pointer.
• The following program increments the variable pointer to access each succeeding element of the array −
Example 7

#include <iostream> for (int i = 0; i < MAX; i++) {


cout << "Address of var[" << i << "] = ";
using namespace std; cout << ptr << endl;
const int MAX = 3;
cout << "Value of var[" << i << "] = ";
int main () { cout << *ptr << endl;
int var[MAX] = {10, 100, 200};
int *ptr; // point to the next location
ptr++;
// let us have array address in pointer. }

ptr = var; return 0;


}
Output: 8

• When the above code is compiled and executed, it produces result something as follows—

Address of var[0] = 0xbfa088b0


Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
Decrementing a Pointer 9

The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data
type as shown below −

for (int i = MAX; i > 0; i--) {


#include <iostream> cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
using namespace std;
const int MAX = 3; cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
int main () {
int var[MAX] = {10, 100, 200}; // point to the previous location
int *ptr;
ptr--;
// let us have address of the last element in pointer. }
ptr = &var[MAX-1]; return 0;
}
Output 10

• When the above code is compiled and executed, it produces result something as follows −
Address of var[3] = 0xbfdb70f8
Value of var[3] = 200
Address of var[2] = 0xbfdb70f4
Value of var[2] = 100
Address of var[1] = 0xbfdb70f0
Value of var[1] = 10
11

Addition of Integer/Constant to Pointer

• You can add an integer to a pointer but you cannot add a pointer to a pointer.
• the integer value is first multiplied by the size of the data type to which the pointer
points, and then the result is added to the pointer’s address.
• You have an integer pointer storing the address ptr = 1000.
• When you add the integer 5 to it with the expression ptr = ptr + 5, the final address
stored in ptr will be calculated as follows: 1000 + sizeof(int) * 5, which is equal to
1020.
12
#include <iostream>
using namespace std;
int main() {
int num = 20;
int* ptr = &num;
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr + 3;
cout << "Adding 3 to ptr: " << ptr << endl;
return 0;
}
13

Subtraction of Integer/constant to Pointer

• If you have an integer pointer storing the address ptr = 1000.


• When you subtract the integer 5 from it with the expression ptr = ptr - 5, the final
address stored in ptr will be calculated as follows: 1000 - sizeof(int) * 5, which is equal
to 980.
14
#include <iostream>
using namespace std;
int main()
{
int num = 100;
int* ptr = &num;
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr - 2;
cout << "Subtract 2 from ptr: " << ptr << endl;
return 0;
}
Subtraction of Two Pointers 15
• When you subtract two pointers that point to the same data type, the result is calculated by finding
the difference between their addresses and then dividing by the size of the data type to determine
how many elements (not bytes) separate the two pointers.
• If ptr1 = 1000 and ptr2 = 1004.
• (ptr2 - ptr1), you get a result of 4 bytes, which is the difference in addresses.
• Since the size of an integer is 4 bytes, you divide the address difference by the size of the data type:
(4 / 4), which equals 1. This means there is an increment of 1 integer-sized element between ptr1 and
ptr2.
• If you have two pointers that point to the same array, you can subtract one pointer from the other.
This operation yields the number of elements in the array that separate the two addresses that the
pointers refer to.
16

#include <iostream>

using namespace std;

int main() {

int x = 6; // Integer variable declaration

int N = 4;

// Pointer declaration

int *ptr1, *ptr2;

ptr1 = &N; // stores the address of N


OUTPUT:-
ptr2 = &x; // stores the address of x ptr1 = 0x7ffd40979c78, ptr2 = 0x7ffd40979c7c
Subtraction of ptr1 & ptr2 is 1

cout << "ptr1 = " << ptr1 << ", ptr2 = " << ptr2 << endl;
Pointer Comparisons 17
• Pointers may be compared by using the following operators: ==, !=, <, >, <=, and >=.

• Pointer comparisons are defined only when the pointers have same data type. Comparison is meaningful
when pointers point to elements of the same array.

• 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.

• The following program modifies the previous example one by incrementing the variable pointer so 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] −
Example 18

#include <iostream> while ( ptr <= &var[MAX - 1] ) {


cout << "Address of var[" << i << "] = ";
using namespace std; cout << ptr << endl;
const int MAX = 3;
cout << "Value of var[" << i << "] = ";
int main () { cout << *ptr << endl;
int var[MAX] = {10, 100, 200};
int *ptr; // point to the previous location
// let us have address of the first element in pointer. ptr++;
i++;
ptr = var; }
int i = 0;
return 0;
}
Output— 19

• When the above code is compiled and executed, it produces result something as follows −
Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200
20
#include <iostream>

using namespace std;

int main()

int num = 10;

int arr[5] = {6,8,9,45,3};

int* ptr1 = arr;

int* ptr2 = &num;

int* ptr3 = &arr[3];


OUTPUT:-
Both point to different memory location
possible to compare but meaningless
ptr1 is before ptr3

// comparing
21

References

• [Link]

• [Link]

• [Link]

You might also like