Jan 16, 2021 at 6:26pm UTC
You are printing the addresses of num1 and num2, which are displayed in hex.
Presumably you want to print the values of num1 and num2 through the pointers you pointed at them.
So you want to print *num1ptr and *num2ptr.
Jan 16, 2021 at 6:26pm UTC
Perform a type conversion.
1 2 3
#include <cstdint>
// ...
std::cout << "address-of num1: " << reinterpret_cast <std::uintptr_t>(&num1) << '\n' ;
Last edited on Jan 16, 2021 at 6:27pm UTC
Jan 16, 2021 at 6:27pm UTC
You're printing the
address of num1, and the
address of num2, which happen to print in hexadecimal format on most compilers.
If you want to print the values, then you should do:
1 2
cout << num1 << endl;
cout << num2 << endl;
If you want to indirectly do this through the pointers, then do
1 2
cout << "num 1 = " << (*num1ptr) << endl;
cout << "num 2 = " << (*num2ptr) << endl;
(the parentheses aren't necessary here)
Last edited on Jan 16, 2021 at 6:28pm UTC
Jan 17, 2021 at 11:48am UTC
Thank you, this helps!
Another one with arrays. Im using pointers and its printing hexas but i want 10 zeros.
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
using namespace std;
int main()
{
int table[10] = { 0 }, * tableptr;
cout << table << endl;
return 0;
}
Even if I dont have pointers, this will print hexas too.
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
using namespace std;
int main()
{
int table[10] = { 0 };
cout << table << endl;
return 0;
}
Last edited on Jan 17, 2021 at 11:49am UTC
Jan 17, 2021 at 11:53am UTC
table is a memory pointer - so you're printing the address of the beginning of the memory used for table. If you want to print the elements of the array, then you need to iterate the array and print each element separately.
Jan 17, 2021 at 12:03pm UTC
So even if I dont use pointers I have to print array each element separately? Is table always memory pointer?
Jan 17, 2021 at 12:50pm UTC
To display the elements:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
using namespace std;
int main()
{
int table[10] {};
for (const auto t : table)
cout << t << " " ;
cout << '\n' ;
}
You can provide a stream inserter overload for the array, so that you can do what you're tried to do. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
include <iostream>
using namespace std;
template <size_t N>
ostream& operator <<(ostream& os, int (&arr)[N])
{
for (const auto & a : arr)
os << a << " " ;
return os;
}
int main()
{
int table[10] {};
cout << table << '\n' ;
}
Last edited on Jan 17, 2021 at 1:03pm UTC
Jan 17, 2021 at 2:24pm UTC
Okey, this helps. I think its better to make a loop for array. Thank you so much!
Jan 18, 2021 at 12:48pm UTC
@Scorpia Your thread title and OP both mention "reference parameters". However, none of the code in your posts has reference parameters, or references of any kind.