Using a pointer to display hexadecimal value

Jul 12, 2018 at 5:59pm
I'm trying to display the memory location in hexadecimal after a value is received from a user.

With my current code the output I get usually gives something like this 0x7fff5a0eb9d0.

Where exactly did I go wrong here?
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
  void get_sales(double *salesArray, string monthsArray[]);
void total_sales(double *salesArray);

int main()
{
    string monthsArray[6] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun"};
    double salesArray[6];

    get_sales(salesArray, monthsArray);
    total_sales(salesArray);

    return 0;
}

void get_sales(double *salesArray, string monthsArray[])
{
    for (int i = 0; i < 6; i++)
    {
        cout << "\n Enter the sales figure for " << monthsArray[i] << " : ";

        cin >> salesArray[i];

        cout << "   The memory location = " << &salesArray[i];

    }
}
Jul 12, 2018 at 6:12pm
you are printing the value of a pointer, the address of a location in hex. That looks about right, should be 8 bytes on a 64 bit machine, which is 16 characters (2 chars per byte in hex).

did you want the data? the data is in salesArray[i] (no &).

also doubles in hex are, remember, in IEEE format and difficult to read.
Last edited on Jul 12, 2018 at 6:13pm
Jul 12, 2018 at 6:32pm
Nope don't want the data from the array. The assignment calls for a display in hexadecimal each time after the user inputs a value.

Here's a screenshot of what it's supposed to look like. https://gyazo.com/06a737ba78ea00f66af31b92b19ea407

But you don't see an issue with what I did? Any reason why I'm not getting the format like the screenshot?
Last edited on Jul 12, 2018 at 6:33pm
Jul 12, 2018 at 6:48pm
I am getting pretty much the same thing.
are you concerned about the hex VALUE? It is random; all you should see is that they are 8 bytes apart for each one.
Last edited on Jul 12, 2018 at 6:51pm
Jul 12, 2018 at 7:13pm
Well no I understand the value is random and I do see that I am getting results that are 8 bytes apart.

I guess I'm more so curious/concerned that my results look like this.
They are much longer than the solution image shows but still 8 bytes apart. https://gyazo.com/6897b215bcb008125d8d72074c445acc

Does OS have an effect on any of this?

Jul 12, 2018 at 7:32pm
oh, I see.

hmm.

try this.

int * test;
cout << sizeof(test) << endl;

also try

cout << " The memory location = " << hex << &salesArray[i];

it probably won't help
what is happening is that leading zeros are being removed by cout because cout ... sigh.

Last edited on Jul 12, 2018 at 7:38pm
Jul 12, 2018 at 7:46pm
So the cout for int *test was 8.

I tried hex and nothing changed.
Jul 12, 2018 at 7:55pm
that is what i expected. I think its just capping leading zeros then. 8 is what I expected to see. I think your code is fine, nothing wrong, and with a great deal of effort we can show leading zeros but do you really need it?
Jul 12, 2018 at 8:08pm
We can call it here as long as the code is fine. Thanks for the helping me though I appreciate it.
Topic archived. No new replies allowed.