-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpointers2.cpp
More file actions
60 lines (55 loc) · 1.11 KB
/
pointers2.cpp
File metadata and controls
60 lines (55 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
class print
{
public:
void printer(int *ptr)
{
cout << *ptr << endl;
}
void printer(char *ptr)
{
cout << *ptr << endl;
}
void printe(void *ptr, char identify)
{
// typecasting the void pointer haha
switch (identify)
{
case 'c':
cout << *((char *)ptr) << endl;
break;
case 'i':
cout << *((int *)ptr) << endl;
break;
default:
cout << "Please enter a valid identifier :(" << endl;
break;
}
}
};
int main()
{
int samples;
print print;
cin >> samples;
while (samples--)
{
// learning void pointers
int n = 3;
int *ptr = &n;
char gul = 'c';
print.printer(ptr);
// or
print.printer(&gul);
print.printe(&n, 'i');
print.printe(&gul, 'c');
// inside main function
void *pter;
int mega = 6;
pter = &mega;
cout << *((int *)pter) << endl;
cout << &n << endl;
}
return 0;
}