I am teaching myself C++. I am currently writing a program to sort 10
numbers. I have the array with values working, and I can print the values
on the screen, (see output); however, when I try to print the values using
the array of pointers to the array values I get the memory addresses,
(see output). How do I fix this code so that I can dereference the pointers
and print the values. If the code is working properly both outputs should
match.
I changed line 34 to... cout << setw(5) << **(pnumbers+i);
...and it worked perfectly.
I don't really understand why that worked.
I believe that this is pointer to pointer notation.
Am I using this notation because I am pointing to an array element?
pnumbers is an array of int*, and i is the offset you want to look at. pnumbers is the address of the first element of the array, so we start there and add i to get to the address of the element we want.
Deference that to get the i-th element. Since that is an int*, and we want the actual int, we need to dereference again.