-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointerConst.cpp
58 lines (48 loc) · 1.09 KB
/
pointerConst.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
#include<iostream>
using namespace std;
//pointer & const
void increment_all (int* start, int* stop)
{
int* current = start;
while (current != stop)
{
++(*current);
++current;
}
}
void print_all (const int* start, const int* stop)
{
const int* current = start;
while (current != stop)
{
cout << *current << "\n";
++current;
}
}
//void pointer : we use it when we dont have the type of pointer (char, int...) and to dereference we have to define its type exemple :
void increase (void* data, int psize)
{
if (psize == sizeof(char))
{
char* pchar;
pchar = (char*)data;
++(*pchar);
} else if (psize == sizeof(int))
{
int* pint;
pint = (int*)data;
++(*pint);
}
}
int main(int argc, char const *argv[])
{
/* int numbers[] = {10, 20, 30};
increment_all(numbers, numbers+3);
print_all(numbers, numbers+3); */
char a = 'x';
int b = 1602;
increase(&a, sizeof(a));
increase(&b, sizeof(b));
cout << a << ", " << b << "\n";
return 0;
}