Skip to content

Commit 6d2bb18

Browse files
Add files via upload
1 parent f70ae64 commit 6d2bb18

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

21_pointerarithmetic.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
// We can easily manipulate the value at address stored by the pointer using pointer. Pointer is certainly a variable which is capable of having both the address as well as the value of the value of the variable whose address it is being storing.
6+
// A pointer must be always initialised even with zero. A pointer which is certainly pointing to an unidentified and unrecognised location is very very risky.
7+
8+
int a = 9;
9+
int *ptr = &a;
10+
11+
// updating the value of the address stored by the pointer
12+
13+
cout<<"Before Updation: "<<*ptr<<endl;// gives 9
14+
(*ptr)++;
15+
cout<<"After Updation: "<<*ptr<<endl; // gives 10
16+
17+
// If we want to create a new copy of the pointer then we can also copy a particular pointer into another pointer
18+
int* ptr2 = ptr;
19+
cout<<endl<<"Address of Pointer 1: "<< ptr<<endl;
20+
cout<<"Address stored by Pointer 2: "<< ptr2<<endl;
21+
cout<<"Value stored by Pointer 1 is: "<< *ptr<<endl;
22+
cout<<"Value stored by Pointer 2 is: "<< *ptr2<<endl;
23+
24+
25+
26+
// updating or manipulating the address stored by the pointer
27+
28+
cout<<endl<<"Address before: "<<ptr<<endl;//supppose address here is 1074
29+
ptr++;
30+
cout<<"Address after: "<<ptr<<endl;// address updated will be 1078 because while updating the address, the address is increased by the size of the type of the pointer. In our case, its integer, so the size it is increased by is 4 bytes, so it becomes 1078.
31+
32+
33+
return 0;
34+
}

21_pointerarithmetic.exe

44.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)