Skip to content

Commit 82d1f2d

Browse files
committed
Pointers: Program 1 to 5
1 parent db04a6f commit 82d1f2d

5 files changed

+293
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Address of Operator (&)
3+
- It is used to find address of variable
4+
- C++ display address in HexaDecimal format (Base 16)
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
10+
int main(){
11+
int x=10;
12+
cout << "[Integer] Address of x [&x]: " << &x << endl;
13+
14+
float y=9.5;
15+
cout << "[Float] Address of y [&y]: " << &y << "\n\n";
16+
17+
// It doesn't work for character variables bcz of operator overloading
18+
char ch = 'A';
19+
cout << "[Character] Address of ch [&ch]: " << ch << endl;
20+
21+
// Explicit typecasting from char* to void*
22+
cout << "[Character] Address of ch [(void*)&ch]: " << (void*)&ch << endl;
23+
return 0;
24+
}
25+
26+
/*
27+
OUTPUT:
28+
29+
[Integer] Address of x [&x]: 0x7ffd67f494e0
30+
[Float] Address of y [&y]: 0x7ffd67f494e4
31+
[Character] Address of ch [&ch]: A
32+
[Character] Address of ch [(void*)&ch]: 0x7ffd67f494df
33+
*/

10_pointers/02_pointers.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Pointers
3+
- Pointer is a variable that stores the address of another variable
4+
Syntax: datatype * variable_name
5+
- The size of pointer doesn't depend upon datatype. It is same for all.
6+
*/
7+
8+
#include <iostream>
9+
using namespace std;
10+
11+
int main(){
12+
int a = 10;
13+
int b = 20;
14+
cout << "&a: " << &a << endl;
15+
cout << "&b: " << &b << "\n\n";
16+
17+
int *p = &a; // declaration + initializaton
18+
int *pt; // declaration [currently storing garbage value]
19+
20+
/*
21+
Sometimes it is useful to make our pointers point to nothing. This is called a null pointer.
22+
We assign a pointer a null value by setting it to address 0.
23+
*/
24+
int *ptr = 0; // NULL pointer
25+
26+
// store the address of variable a
27+
ptr = &a; // assignment
28+
cout << "ptr: " << ptr << endl;
29+
30+
// Re-assign another address to ptr pointer
31+
ptr = &b;
32+
cout << "ptr: " << ptr << endl;
33+
34+
return 0;
35+
}
36+
37+
/*
38+
OUTPUT:
39+
40+
&a: 0x7fff1ce2db00
41+
&b: 0x7fff1ce2db04
42+
43+
ptr: 0x7fff1ce2db00
44+
ptr: 0x7fff1ce2db04
45+
46+
47+
More About Pointers:
48+
49+
- Pointer & Arithmetic
50+
> Addition, Multiplication, Division of two addresses doesn’t make any sense
51+
> Addition of an address by a constant integer value
52+
i.e. ptr +5 means address of cell which is 5 * sizeof(*ptr) away from ptr.
53+
> Similar for subtraction
54+
> Again Multiplying/Dividing an address with some constant value doesn’t make any sense
55+
> Subtracting two address of same type would give you number of elements between them
56+
57+
- Arrays & Pointers
58+
> An Array is actually a pointer that points to the first element of the array!
59+
Because the array variable is a pointer, you can dereference it, which returns array element 0.
60+
> a[i] is same as *(a + i)
61+
62+
- Difference – Arrays & Pointers
63+
- The sizeof operator
64+
> sizeof(array) returns the amount of memory used by all elements in array
65+
> sizeof(pointer) only returns the amount of memory used by the pointer variable itself
66+
67+
- The & operator
68+
> &array is an alias for &array[0] and returns the address of the first element in array
69+
> &pointer returns the address of pointer
70+
71+
- String literal initialization of a character array
72+
> char array[] = “abc” sets the first four elements in array to ‘a’, ‘b’, ‘c’, and ‘\0′
73+
> char *pointer = “abc” sets pointer to the address of the “abc” string
74+
(which may be stored in read-only memory and thus unchangeable)
75+
76+
- Assignment/Re-assigment
77+
> Pointer variable can be assigned a value whereas array variable cannot be.
78+
Eg: int a[10];
79+
int *p;
80+
p=a; // legal
81+
a=p; // illegal
82+
83+
- Arithmetic
84+
> Arithmetic on pointer variable is allowed but array can’t be incremented/decremented.
85+
Eg: p++; // legal
86+
a++; // illegal
87+
*/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Topic - Dereference Operator (*)
3+
4+
asterisk (*) has many use cases :-
5+
1. For multiplication // 5*2
6+
2. to declaring pointer // int * ptr;
7+
3. to dereference any address // *ptr + 1
8+
9+
Dereference:
10+
- In simple words, dereferencing means accessing the value from a certain memory location against
11+
which that pointer is pointing.
12+
- So, by dereferencing pointer we can access the variable, in which they are pointing to.
13+
14+
*/
15+
16+
#include <iostream>
17+
using namespace std;
18+
19+
int main(){
20+
int a = 10;
21+
int *ptr = &a; // declaration & initilization
22+
23+
cout << "&a - " << &a << endl; // OUTPUT: (Address of bucket "a")
24+
cout << "ptr - " << &a << "\n\n"; // OUTPUT: (Address of bucket "a")
25+
26+
// dereferencing
27+
cout << "*(&a) - " << *(&a) << endl; // OUTPUT: 10 (value -> address a)
28+
cout << "*(ptr) - " << *(ptr) << "\n\n"; // OUTPUT: 10 (value -> address stored at ptr)
29+
30+
cout << "*(&ptr) - " << *(&ptr) << endl; // OUTPUT: (Address of bucket "a") [=> value stored at ptr) => value at &ptr]
31+
cout << "&(*ptr) - " << &(*ptr) << "\n\n"; // OUTPUT: (Address of bucket "a") [i.e *ptr = bucket "a"]
32+
33+
// Know the difference :-
34+
cout << "*(ptr) + 1 - " << *(ptr)+1 << endl; // OUTPUT: 11 (i.e 10+1)
35+
cout << "*(ptr+1) - " << *(ptr+1) << "\n\n"; // OUTPUT: Unknown/Garbage Value
36+
// (value at next memory address)
37+
38+
// Double Pointer
39+
int ** newptr = &ptr;
40+
cout << "Double Pointer: " << endl;
41+
cout << "newptr: " << newptr << endl; // OUTPUT: (address of ptr)
42+
cout << "&ptr: " << &ptr << endl; // OUTPUT: (address of ptr)
43+
44+
return 0;
45+
}
46+
47+
/*
48+
OUTPUT:
49+
50+
&a - 0x7ffe49b6db94
51+
ptr - 0x7ffe49b6db94
52+
53+
*(&a) - 10
54+
*(ptr) - 10
55+
56+
*(&ptr) - 0x7ffe49b6db94
57+
&(*ptr) - 0x7ffe49b6db94
58+
59+
*(ptr) + 1 - 11
60+
*(ptr+1) - 1236720532
61+
62+
Double Pointer:
63+
newptr: 0x7ffe49b6db98
64+
&ptr: 0x7ffe49b6db98
65+
*/

10_pointers/04_pass_by_reference.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Topic - Pass by Reference Using Pointers
3+
4+
Pass by Value : Passing the copy of variable
5+
Pass by Reference: Passing the address of variable
6+
7+
*/
8+
9+
#include <iostream>
10+
using namespace std;
11+
12+
// call by value
13+
void increment_1(int num){ // variable
14+
num = num +1;
15+
cout << "[Call by Value] a: "<< num << endl;
16+
}
17+
18+
// call by reference
19+
void increment_2(int *num){ // pointer
20+
*num = *num +1;
21+
cout << "[Call by reference] b:"<< *num << endl;
22+
}
23+
24+
int main(){
25+
int a = 10;
26+
int b = 10;
27+
28+
cout << "a : "<< a << endl;
29+
increment_1(a); // Passing the copy of variable
30+
cout << "[Inside Main] a: "<< a << endl;
31+
32+
cout << "\nb : "<< b << endl;
33+
increment_2(&b); // Passing the address of variable
34+
cout << "[Inside Main] b: "<< b << endl;
35+
36+
37+
return 0;
38+
}
39+
40+
/*
41+
OUTPUT:
42+
a : 10
43+
[Call by Value] a: 11
44+
[Inside Main] a: 10
45+
46+
b : 10
47+
[Call by reference] b:11
48+
[Inside Main] b: 11
49+
*/

10_pointers/05_reference_variable.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Topic - References in C++
3+
4+
Reference Variable
5+
- When a variable is declared as a reference, it becomes an alternative name for an existing variable.
6+
- A variable can be declared as a reference by putting ‘&’ in the declaration.
7+
*/
8+
9+
#include <iostream>
10+
using namespace std;
11+
12+
13+
int main(){
14+
int num = 10;
15+
16+
int &ref = num; // ref is a reference to num
17+
cout << "ref : "<< ref << endl;
18+
19+
ref = 20; // value of num(alias name ref) is now changed to 20.
20+
cout << "ref : "<< ref << endl;
21+
22+
num = 30; // value of num is now changed to 20.
23+
cout << "ref : "<< ref << endl;
24+
25+
return 0;
26+
}
27+
28+
/*
29+
OUTPUT:
30+
ref : 10
31+
ref : 20
32+
ref : 30
33+
34+
35+
More About References Variable:
36+
37+
- Reference Variable:
38+
> A reference variable is an alias, that is, another name for an already existing
39+
variable/memory instance.
40+
> Once a reference is initialized with a variable, either the variable name or the reference
41+
name may be used to refer to the variable.
42+
> The reference variable once defined to refer to a variable cannot be changed to
43+
point to other variable.
44+
45+
- Defining Reference Variable:
46+
> Reference variables are defined by using '&' symbol in the definition.
47+
> Since they do not have any storage of their own and are just another name for the
48+
existing storage, they need to initialized before using them.
49+
> Eg: int x;
50+
int &y = x;
51+
The exisiting memory X will now also have another name Y.
52+
53+
- Reference Variable VS Pointer Variable
54+
> You cannot have NULL references. You must always be able to assume that a reference
55+
is connected to a legitimate piece of storage.
56+
> Once a reference is initialized to an object, it cannot be changed to refer to another object.
57+
Pointers can be pointed to another object at any time.
58+
> A reference must be initialized when it is created. Pointers can be initialized at any time.
59+
*/

0 commit comments

Comments
 (0)