pointers

I need help with the last part of my code:
this is the output I need but im not sure where to start


dynamic (uses: p_natural, Number("one"), Number("two"):
address of p_natural is: &0x22fda8 contents of p_natural is: 0x55ffa0
address of p_natural->name is: &0x55ffa0 contents of p_natural->name is: one
address of p_natural->next is: &0x55ffa8 contents of p_natural->next is: 0x55fbb0
<follow link to next Number on p_natural>
address of p_natural->next->name is: &0x55fbb0 contents of p_natural->next->name is: two
address of p_natural->next->next is: &0x55fbb8 contents of p_natural->next->next is: 0



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
#include <iostream>
#include <iomanip>
using namespace std;

class Number { // modify class Number if desired to provide constructors, etc.
  public:
    string name;     // name of the Number
    Number *next;    // pointer to the next Number in the list
};

#define show_addr_value(var, width) \
  cout<<"address of " <<setw(width)<<left<<#var<<" is: &"<<&var<<"  "     \
      <<"contents of "<<setw(width)<<left<<#var<<" is: "<<var<<endl;
#define show_addr(var, width) \
  cout<<"address of " <<setw(width)<<left<<#var<<" is: &"<<&var<<endl;

int main () {
    cout << "Output from Lab10 memory diagram on pointers:\n"
    <<"---------------------------------------------\n";
    float price = 0;
    float *p_price = &price;
    *p_price = 18.65;
    show_addr_value(price, 8);
    show_addr_value(p_price, 8);
    cout << "\nThe contents of *p_price is: " << *p_price << endl;
    
    // contents of pi
    cout << "<========================" << endl;
    double PI = 3.141592;
    double *p_PI  = &PI;
    double PI_div_2 = *p_PI/2;
    show_addr_value(PI, 8);
    show_addr_value(PI_div_2, 8);
    show_addr_value(p_PI, 8);
    cout << "\nThe contents of *p_PI is:    " << *p_PI << endl;
    
    // acceid
    cout << "<========================" << endl;
    int ACCeID = 1234567;
    int *p_ACCeID = &ACCeID;
    show_addr_value(ACCeID, 8);
    show_addr_value(p_ACCeID, 8);
    cout << "\nThe contents of *p_ACCeID is: " << *p_ACCeID << endl;

    //
    cout << "<========================" << endl;
    int *p_amount;
    p_amount = new int;
    *p_amount = 2468;
    show_addr_value(p_amount, 8);
    cout<<endl;
    cout << "The contents of *p_amount is: " << (dec) << *p_amount << endl;
    delete p_amount;
    cout << "After delete, the contents of p_amount is: " << p_amount << endl;
    p_amount = nullptr;
    cout << "After reset to nullptr, the contents of p_amount is: " << p_amount << endl;

    //
    cout << "<========================" << endl;
    int *pArray, arraySize = 3;
    pArray = new int [arraySize];
    for(int count =0, val=100; count < arraySize; count++, val+=100)
    {
        pArray[count] = val;
    }
    show_addr_value(pArray, 8);
    show_addr_value(pArray[0], 8);
    show_addr_value(pArray[1], 8);
    show_addr_value(pArray[2], 8);
    delete [] pArray;
    cout<<endl;
    cout << "After delete [], the contents of pArray is: " << pArray << endl;
    pArray = nullptr;
    cout << "After reset to nullptr, the contents of pArray is: " << pArray << endl;
    
    //
    cout << "<========================" << endl;
    int *pArray2, arraySize2 = 3;
    pArray2 = new int [arraySize2];
    for(int count =0, val=1111; count < arraySize; count++, val+=1111)
    {
        pArray2[count] = val;
    }
    show_addr_value(pArray2, 8);
    show_addr_value(pArray2[0], 8);
    show_addr_value(pArray2[1], 8);
    cout << endl;
    cout << "No need to delete Array2, it is on the stack, not the heap" << endl;
    
    //
    cout << "<========================" << endl;
    int *big_buffer;
    big_buffer = new int;
    show_addr_value(big_buffer, 8);
    delete big_buffer;
    cout<< endl;
    cout << "After delete [], the contents of big_buffer is: " << big_buffer << endl;
    big_buffer = nullptr;
    cout << "After reset to nullptr, the contents of big_buffer is: " << big_buffer << endl;

    //
    cout << "<========================" << endl;
    int emptyList=0;
    show_addr_value(emptyList, 8);
    
    //
    cout << "<========================" << endl;
    cout<<"static (uses: Number zed):\n";
    Number zed;
    show_addr(zed, 8);
    zed.name = "zero";
    show_addr_value(zed.name, 8);
    zed.next = 0;
    show_addr_value(zed.next, 8);
    //
    cout << "<========================" << endl;
    cout<<"dynamic (uses: p_natural, Number(\"one\"), Number(\"two\"):\n";
int *p_natural;
    show_addr_value(p_natural, 8);
    
  return 0;
} // end of main 

Last edited on
p_natural, on line 118, is not initialized.
what are you supposed to do with it? You can't get its value until you point it to valid memory to hold said value...
check your instructions on how to deal with this variable.
could be a new statement, or assign to an existing value, or whatever...
Last edited on
¿what's your program supposed to do?
Pointers are easy to mess up, and can be hard to figure out where things go wrong.

Maybe a bit of a refresher on pointers might be helpful:

https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/

This looks like an attempt to create a custom version of a dynamic array.

https://www.learncpp.com/cpp-tutorial/dynamic-memory-allocation-with-new-and-delete/ and https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/ might help give you a leg-up.
thank you for those links I figured it out now
Topic archived. No new replies allowed.