-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.cpp
More file actions
122 lines (110 loc) · 3.58 KB
/
pointers.cpp
File metadata and controls
122 lines (110 loc) · 3.58 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
* Copyright (C) 2013 by Alexey S. Malakhov *
* brezerk@gmail.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/*
* pointers.c
*
* A simple poiters use cases.
* With gdb debug output.
*
*/
#include <iostream>
#include <stdlib.h>
void foo0(int a0){
std::cout << a0 << std::endl;
return;
}
void foo1(int &a1){
std::cout << a1 << std::endl;
return;
}
void foo2(int *a2){
std::cout << a2 << std::endl;
return;
}
int main(){
/*
* Val: 10
* Adr: (int *) 0x7fffffffdfa4
*/
int a = 10;
/*
* Val: (int *) 0x7fffffffdfa4
* Adr: (int **) 0x7fffffffdfa8
*/
int *b = &a;
/*
* Copy a -> a0
*
* Val: 10
* Adr: (int *) 0x7fffffffdf8c
*/
foo0(a);
//foo0(&a); invalid conversion from ‘int*’ to ‘int’
//foo0(*a); invalid type argument of unary ‘*’ (have ‘int’)
/*
* Copy an adress of a and place it to a1
*
* Val: (int &) @0x7fffffffdfa4: 10
* Adr: (int *) 0x7fffffffdfa4
*/
foo1(a);
//foo1(&a); invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int*’
//foo1(*a); invalid type argument of unary ‘*’ (have ‘int’)
//foo2(a); invalid conversion from ‘int’ to ‘int*’
/*
* Create a pointer a2 to address of a
*
* Val: (int *) 0x7fffffffdfa4
* Adr: (int **) 0x7fffffffdf88
*/
foo2(&a);
//foo2(*a); invalid type argument of unary ‘*’ (have ‘int’)
//foo0(b); invalid conversion from ‘int*’ to ‘int’
//foo0(&b); invalid conversion from ‘int**’ to ‘int’
/*
* Take an a value of pointer and copy it to a0
* i.e.: b -> &a and *b = 10
*
* Val: 10
* Adr: (int *) 0x7fffffffdf8c
*
*/
foo0(*b);
//foo1(b); invalid initialization of reference of type ‘int&’ from expression of type ‘int*’
//foo1(&b); invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int**’
/*
* Take an a value of a pointer and copy it's address to a1
*
* Val: (int &) @0x7fffffffdfa4: 10
* Adr: (int *) 0x7fffffffdfa4
*
*/
foo1(*b);
// pointer to 10
/*
* Take an a pointer and copy it's addres to a2
*
* Val: (int *) 0x7fffffffdfa4
* Adr: (int **) 0x7fffffffdf88
*/
foo2(b);
//foo2(&b); cannot convert ‘int**’ to ‘int*’ for argument ‘1’ to ‘void foo2(int*)’
//foo2(*b); invalid conversion from ‘int’ to ‘int*’
return 0;
}