L4_Pointer Arithmetic in C++.pptxhwhwjwjw

DhruvBharadwaj4 3 views 21 slides Mar 04, 2025
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Buggati


Slide Content

SDF II(15B11CI211) EVEN Semester 202 5 2 nd Semester , First Year Jaypee Institute Of Information Technology (JIIT), Noida

Lecture 4 – Pointers arithmetic in C++

Pointers 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: 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: Increment Operator ++, Decrement Operator --, Addition , Subtraction Comparison (>, >=, <, <=, ==)

POINTER ARITHMETIC: 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 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 #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int * ptr ; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i ++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl ; cout << "Value of var[" << i << "] = "; cout << * ptr << endl ; // point to the next location ptr ++; } return 0; }

Output: 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 The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type as shown below − #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int * ptr ; // let us have address of the last element in pointer. ptr = &var[MAX-1]; for (int i = MAX; i > 0; i--) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the previous location ptr--; } return 0; }

Output 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

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.

#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; }

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.

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

#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 ptr2 = &x; // stores the address of x cout << "ptr1 = " << ptr1 << ", ptr2 = " << ptr2 << endl ; // Subtraction of ptr2 and ptr1 x = ptr2 - ptr1; cout << "Subtraction of ptr1 & ptr2 is " << x << endl ; return 0; } OUTPUT:- ptr1 = 0x7ffd40979c78, ptr2 = 0x7ffd40979c7c Subtraction of ptr1 & ptr2 is 1

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

Output— 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

#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]; // comparing if (ptr1 != ptr2) { cout << "Both point to different memory location"<< endl ; } if (ptr1 < ptr2) { cout << "possible to compare but meaningless"<< endl ; } if (ptr1 < ptr3) { cout << "ptr1 is before ptr3"; } return 0; } OUTPUT:- Both point to different memory location possible to compare but meaningless ptr1 is before ptr3

References https://www.tutorialspoint.com/cplusplus/cpp_pointer_arithmatic.htm http://www.infobrother.com/Tutorial/C++/C++-Pointer-Arithmetic https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/
Tags