-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.cpp
More file actions
executable file
·208 lines (163 loc) · 5.78 KB
/
pointer.cpp
File metadata and controls
executable file
·208 lines (163 loc) · 5.78 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// hello.cpp by Bill Weinman <http://bw.org/>
#include <cstdio> // or use <iostream>
#include <iostream>
using namespace std;
void iterateThroughArray(int *array, int size){
cout<< "use function" << endl;
for(int i = 0; i<size; i++){
cout<< *array << " ";
array++;
}
}
void iterateThroughMatrix(int matrix[][4], int height, int width){
cout << "use the function" << endl;
cout << matrix << endl;
for(int i= 0 ; i < height; i++){
for( int j=0; j < width; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
cout << "-------------------------------------------------------------" << endl;
cout << "Pointer iteration on array" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "" << endl;
int a[6] = {0,1,2,3,4,5};
int *ptr = &a[0];
while(*ptr != 5){
ptr++;
std::cout << "value: " << *ptr << std::endl;
std::cout << "Address: " << &ptr << std::endl;
std::cout << "Pointer: " << ptr << std::endl;
}
//Use a function
iterateThroughArray(a,6);
cout << "" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "Pointer iteration on 2D matrix" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "" << endl;
int matrix[3][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
int *matrixPtr = &matrix[0][0];
cout << "Matrix [0] " << matrix[0] << endl;
cout << "MatrixPtr: " << matrixPtr << endl;// same as matrix[0]
cout << "Matrix [0][0] " << matrix[0][0] << endl;
int matrixHeight = sizeof(matrix) / sizeof(matrix[0]);
int matrixWidth = sizeof(matrix[0]) / sizeof(matrix[0][0]);
cout << "Matrix Height: " << matrixHeight << endl;// same as matrix[0]
cout << "Matrix Width: " << matrixWidth << endl;
//normal iteration
cout << ""<< endl;
cout << "Normal array Iteration" << endl;
for(int i= 0 ; i < matrixHeight; i++){
for( int j=0; j < matrixWidth; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
//pass to a function
iterateThroughMatrix(matrix,3,4);
//Playing with pointer
cout << "" << endl;
cout << "Playing with pointer" << endl;
cout << "" << endl;
cout << "Using matrix index pointer" << endl;
for(int i = 0; i < matrixHeight; i++){
for(int j = 0; j < matrixWidth; j++){
cout << *matrix[i] << " "; //Printing first element of each row
}
cout<<endl;
}
cout << "" << endl;
cout << "Using pointer on first element" << endl;
for(int i = 0; i < matrixHeight; i++){
for(int j = 0; j < matrixWidth; j++){
cout << *matrixPtr << " ";
matrixPtr++;
}
cout<<endl;
}
cout << "" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "Pointer iteration on 3D matrix" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "" << endl;
int matrix3D[3][3][4] = {
{
{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}
}
};
int *matrixPtr3D = &matrix3D[0][0][0];
cout << "Matrix [0] " << matrix3D[0] << endl;
cout << "Matrix [0][0] " << matrix3D[0][0] << endl;
cout << "MatrixPtr: " << matrixPtr3D << endl;// same as matrix[0]
cout << "Matrix [0][0][0] " << matrix3D[0][0][0] << endl;
//Pointer values
cout << "*Matrix [0] " << *matrix3D[0] << endl; //point to pointer matrix3D[0][0]
cout << "**Matrix [0] " << **matrix3D[0] << endl; //point to pointer matrix3D[0][0]
cout << "*Matrix [0][0] " << *matrix3D[0][0] << endl;
cout << "*MatrixPtr: " << *matrixPtr3D << endl;// same as matrix[0]
int matrix3dDepth = sizeof(matrix3D) / sizeof(matrix3D[0]);
int matrix3dHeight = sizeof(matrix3D[0]) / sizeof(matrix3D[0][0]);
int matrix3dWidth = sizeof(matrix3D[0][0]) / sizeof(matrix3D[0][0][0]);
cout << "Matrix Depth: " << matrix3dDepth << endl;// same as matrix[0]
cout << "Matrix Height: " << matrix3dHeight << endl;// same as matrix[0]
cout << "Matrix Width: " << matrix3dWidth << endl;
//normal iteration
cout << ""<< endl;
cout << "Normal array Iteration" << endl;
for(int k = 0; k< matrix3dDepth; k++){
for(int i= 0 ; i < matrix3dHeight; i++){
for( int j=0; j < matrix3dWidth; j++){
cout << matrix3D[k][i][j] << " ";
}
cout << endl;
}
cout<<""<<endl;
}
//Playing with pointer
cout << "" << endl;
cout << "Playing with pointer" << endl;
cout << "" << endl;
cout << "Using matrix index pointer" << endl;
for(int k = 0; k< matrix3dDepth; k++){
for(int i = 0; i < matrixHeight; i++){
for(int j = 0; j < matrixWidth; j++){
cout << **matrix3D[i] << " "; //Printing first element of each row
}
cout<<endl;
}
}
cout << "" << endl;
cout << "Using pointer on first element" << endl;
for(int k = 0; k< matrix3dDepth; k++){
for(int i = 0; i < matrix3dHeight; i++){
for(int j = 0; j < matrix3dWidth; j++){
cout << *matrixPtr3D << " ";
matrixPtr3D++;
}
cout<<endl;
}
cout<<""<<endl;
}
return 0;
}