-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_pointer_arrays.cpp
More file actions
71 lines (60 loc) · 2.63 KB
/
dynamic_pointer_arrays.cpp
File metadata and controls
71 lines (60 loc) · 2.63 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
#include <iostream>
#include "student.hpp"
using namespace std;
int main() {
// dynamic array of int pointers
int** value_locations = new int*[10];
// dynamic array of student pointers
student** cpsc121_locations = new student*[10];
int i = 1;
// dynamic creation of int stored as an element of the array that is necessary
// before assigning or retrieving values
value_locations[0] = new int;
*value_locations[0] = 5;
// pointer arithmetic version of dynamically creating an int and assigning
// values
*(value_locations + i) = new int;
*(*(value_locations + i)) = 10;
// Each element is an address that needs to be deleted. We only delete 2
// because we only allocated 2 ints
delete value_locations[0];
delete value_locations[1];
value_locations[0] = nullptr;
value_locations[0] = nullptr;
// The array should be deleted only after its individual elements are deleted
delete[] value_locations;
value_locations = nullptr;
// dynamic creation of a student stored as an element of the array that is
// necessary before calling its methods
cpsc121_locations[0] = new student;
// The following are variations for accessing the student object's methods
// pointer arithmetic and bracket notations
(*cpsc121_locations[0]).set_name("Julian");
// bracket notations and arrow notation
cpsc121_locations[0]->set_id(12345);
cpsc121_locations[0]->display();
// using pointer arithmetic to store the dynamically created object
*(cpsc121_locations + i) = new student;
// using pointer arithmetic and the dot notation to access the student
// object's methods
(*(*(cpsc121_locations + i))).set_name("Adriana");
// using pointer arithmetic and the arrow notation to access the student
// object's methods
(*(cpsc121_locations + i))->set_id(6789);
(*(cpsc121_locations + i))->display();
cpsc121_locations[i] = new student;
(*cpsc121_locations[i]).set_name("Julian"); // bracket notation and dereferencing to call method
cpsc121_locations[i]->set_id(12345); // bracket notation and arrow notation to call method
(*(*(cpsc121_locations+i))).display(); // pointer arithmetic and dereferencing to call method
(*(cpsc121_locations+i))->display(); // pointer arithmetic and arrow notation to call method
// Each element is an address that needs to be deleted. We only delete 2
// because we only allocated 2 student objects
delete cpsc121_locations[0];
delete cpsc121_locations[1];
cpsc121_locations[0] = nullptr;
cpsc121_locations[1] = nullptr;
// The array should be deleted only after its individual elements are deleted
delete[] cpsc121_locations;
cpsc121_locations = nullptr;
return 0;
}