Pointers
✔ Pointers storeaddress 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;
4.
POINTER ARITHMETIC:
• pointeris 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 (>, >=, <, <=, ==)
5.
POINTER ARITHMETIC:
• Whenyou 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.
6.
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 −
7.
Example
#include <iostream>
using namespacestd;
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;
}
8.
Output:
• When theabove 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
9.
Decrementing a Pointer
Thesame 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;
}
10.
Output
• When theabove 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/Constantto 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 namespacestd;
int main() {
int num = 20;
int* ptr = #
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr + 3;
cout << "Adding 3 to ptr: " << ptr << endl;
return 0;
}
13.
Subtraction of Integer/constantto 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 namespacestd;
int main()
{
int num = 100;
int* ptr = #
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr - 2;
cout << "Subtract 2 from ptr: " << ptr << endl;
return 0;
}
15.
Subtraction of TwoPointers
• 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 namespacestd;
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
17.
Pointer Comparisons
• Pointersmay 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] −
18.
Example
#include <iostream>
using namespacestd;
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;
}
19.
Output—
• When theabove 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 namespacestd;
int main()
{
int num = 10;
int arr[5] = {6,8,9,45,3};
int* ptr1 = arr;
int* ptr2 = #
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