-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpointers.cpp
215 lines (157 loc) · 5.69 KB
/
pointers.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
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
207
208
209
210
211
212
213
214
215
//*****************************************************************************************************
// Pointer and Array Manipulation
//
// This program demonstrates various uses of arrays and pointers, including printing the values
// and addresses of array elements, getting user input to access specific array elements, and
// performing pointer arithmetic to add the values of two array elements.
//
//*****************************************************************************************************
#include <iostream>
using namespace std;
//*****************************************************************************************************
void f1(int nums[], int size);
void f2(int *ptr, int size);
void f3(int nums[], int size);
void f4(int nums[], int size);
//*****************************************************************************************************
int main() {
const int SIZE = 5;
int numbers[SIZE] = {10, 22, 34, 48, 59};
int *ptr = numbers; // ptr points to the first element in the array
f1(numbers, SIZE);
f2(ptr, SIZE);
f3(numbers, SIZE);
f4(numbers, SIZE);
return 0;
}
//*****************************************************************************************************
void f1(int nums[], int size) {
cout << "-----------------------------\n"
<< " Values and Addresses: Array\n"
<< "-----------------------------" << endl;
for (int i = 0; i < size; ++i)
cout << "\nValue of index " << i << ": " << nums[i]
<< "\nAddress of index " << i << ": " << &nums[i] << endl;
}
//*****************************************************************************************************
void f2(int *ptr, int size) {
cout << "\n-------------------------------\n"
<< " Values and Addresses: Pointer\n"
<< "-------------------------------" << endl;
for (int i = 0; i < size; ++i)
cout << "\nValue of index " << i << ": " << *(ptr + i)
<< "\nAddress of index " << i << ": " << ptr + i << endl;
}
//*****************************************************************************************************
void f3(int nums[], int size) {
const int SIZE = 3;
int *p = nums;
int input;
cout << "\n------------------------------------------------------------\n"
<< " Enter three integer values within the range of the index\n"
<< "------------------------------------------------------------" << endl;
for (int i = 0; i < SIZE; ++i) {
while (true) {
cout << "\nEnter value: ";
cin >> input;
if (input >= 0 && input < size) {
p = &nums[input];
break;
} else {
cerr << "\nError: Invalid Entry\n";
}
}
cout << "Value of index " << input << ": " << *p
<< "\nAddress of index " << input << ": " << p << endl;
}
}
//*****************************************************************************************************
void f4(int nums[], int size) {
int *p1 = nums,
*p2 = nums;
int input1,
input2;
cout << "\n----------------------------------------------------------\n"
<< " Enter two integer values within the range of the index\n"
<< "----------------------------------------------------------" << endl;
while (true) {
cout << "\nEnter value: ";
cin >> input1;
if (input1 >= 0 && input1 < size) {
p1 = &nums[input1];
break;
} else {
cerr << "\nError: Invalid Entry\n";
}
}
cout << "Value of index " << input1 << ": " << *p1 << endl;
while (true) {
cout << "\nEnter value: ";
cin >> input2;
if (input2 >= 0 && input2 < size) {
p2 = &nums[input2];
break;
} else {
cerr << "\nError: Invalid Entry\n";
}
}
cout << "Value of index " << input2 << ": " << *p2
<< "\n\nThe sum of index " << input1 << " and " << input2 << " is " << *p1 + *p2 << endl;
}
//*****************************************************************************************************
/*
-----------------------------
Values and Addresses: Array
-----------------------------
Value of index 0: 10
Address of index 0: 0x83351ffc60
Value of index 1: 22
Address of index 1: 0x83351ffc64
Value of index 2: 34
Address of index 2: 0x83351ffc68
Value of index 3: 48
Address of index 3: 0x83351ffc6c
Value of index 4: 59
Address of index 4: 0x83351ffc70
-------------------------------
Values and Addresses: Pointer
-------------------------------
Value of index 0: 10
Address of index 0: 0x83351ffc60
Value of index 1: 22
Address of index 1: 0x83351ffc64
Value of index 2: 34
Address of index 2: 0x83351ffc68
Value of index 3: 48
Address of index 3: 0x83351ffc6c
Value of index 4: 59
Address of index 4: 0x83351ffc70
------------------------------------------------------------
Enter three integer values within the range of the index
------------------------------------------------------------
Enter value: 6
Error: Invalid Entry
Enter value: 7
Error: Invalid Entry
Enter value: 0
Value of index 0: 10
Address of index 0: 0x83351ffc60
Enter value: 1
Value of index 1: 22
Address of index 1: 0x83351ffc64
Enter value: 2
Value of index 2: 34
Address of index 2: 0x83351ffc68
----------------------------------------------------------
Enter two integer values within the range of the index
----------------------------------------------------------
Enter value: 5
Error: Invalid Entry
Enter value: 6
Error: Invalid Entry
Enter value: 0
Value of index 0: 10
Enter value: 1
Value of index 1: 22
The sum of index 0 and 1 is 32
*/