-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes_1_pointers.cpp
98 lines (70 loc) · 2.56 KB
/
notes_1_pointers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
/*
POINTERS.
1. What is a pointer?
A pointer is an variable that stores
memory address as its value.
Thats all it is.
2. What is a double pointers?
A pointer that points to the memory address
of some other pointer, is called a double
pointer. There can be triple, qudruple... pointers.
*/
int main()
{
int a = 10;
int *pointer = &a;
// pointer now points to the memory address of a.
// we can access the memory address using pointer.
// we can access the value using *pointer.
cout << "Address of a: pointer => " << pointer << endl;
cout << "Value of a: *pointer => " << *pointer << endl;
// double pointer -> we need to define double pointer
// using ** sign.
int **dPointer = &pointer;
// this dPointer is now a double pointer,
// that simply stores the memory address of
// the first pointer.
// dPointer will now store the memory address
// of the first pointer.
// the value of the dPointer will be the memory
// address of a.
// Therefore, we can access the value of dPointer
// using **dPointer.
cout << "dPointer: " << dPointer << endl;
cout << "*dPointer: " << *dPointer << endl;
cout << "**dPointer: " << **dPointer << endl;
// Similarly there can be triple pointers.
int ***tPointer = &dPointer;
cout << tPointer << endl; // mem address of dPointer.
cout << *tPointer << endl; // dPointer.
// These are the same.
cout << "dPointer: " << dPointer << endl;
cout << "*tPointer: " << *tPointer << endl;
// Pointers can only store memory address.
// It gives address if we try to set it to something
// else.
// int *pointer = 12; // int cannot be used to refer *int
// the datatype used with pointer describes the
// datatype of the value for which the pointer is
// storing the address.
// therefore the following are valid:
string str = "Aryan Jain";
string *pointer_s = &str;
cout << "string Mem: " << pointer_s << endl;
cout << "string Val: " << *pointer_s << endl;
char c = 'a';
char *pointer_c = &c;
cout << "char Mem: " << pointer_c << endl;
cout << "char Val: " << *pointer_c << endl;
float f = 3.17238457236594;
float *pointer_f = &f;
cout << "float Mem: " << pointer_f << endl;
cout << "float Val: " << *pointer_f << endl;
double d = 3.17238457236594;
double *pointer_d = &d;
cout << "double Mem: " << pointer_d << endl;
cout << "double Val: " << *pointer_d << endl;
return 1;
}