-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpointers1.cpp
63 lines (48 loc) · 1.44 KB
/
pointers1.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
#include <iostream>
//look at this file until you understand everything
//it's very important
void add_two(int& a){
std::cout<<"function with reference was called" <<std::endl;
a=a+2;
}
void add_two(int* a){
std::cout<<"function with pointer was called" <<std::endl;
*a=*a+2;
}
int main(){
int a{77};
//initialization of reference is obligatory
int& c{a};//c becomes an alias for a
//initialization of pointer is not obligatory, but done "to be safe"
//it is NULL in c, don't use NULL in c++
int* b{nullptr};
b=&a;
std::cout<<"a is: "<<a<<std::endl;
std::cout<<"c is: "<<c<<std::endl;
std::cout<<"address of a is: "<<&a<<std::endl;
std::cout<<"address of c is: "<<&c<<std::endl;
//let's change a:
a=a+2;
std::cout<<"a is: "<<a<<std::endl;
std::cout<<"c is: "<<c<<std::endl;
//let's change a:
c=c+2;
std::cout<<"a is: "<<a<<std::endl;
std::cout<<"c is: "<<c<<std::endl;
//what is b
std::cout<<"b is: "<<b<<std::endl;
std::cout<<"b points to value "<<*b<<std::endl;
//let's change a with b
*b=a+2;
std::cout<<"a is: "<<a<<std::endl;
//let's test functions:
add_two(a);
std::cout<<"a is: "<<a<<std::endl;
add_two(c);
std::cout<<"a is: "<<a<<std::endl;
add_two(&a);
std::cout<<"a is: "<<a<<std::endl;
add_two(b);
std::cout<<"a is: "<<a<<std::endl;
return 0;
}