C++: hex numbers and reference parameters

Jan 16, 2021 at 6:15pm
Hi,
I'm playing with reference and pointer parameters. Problem is when i print it, it gives me only hex numbers. I want it to print as a decimals.

Here is part of my code. Is there any easy way to fix this?

1
2
3
4
5
6
7
int num1 = 50, num2 = 100;

	int* num1ptr = &num1;
	int* num2ptr = &num2;

	cout << "num 1 = " << &num1 << endl; // This should be print 50
	cout << "num 2 = " << &num2 << endl; // And this 100. But noo. 
Jan 16, 2021 at 6:26pm
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
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
Jan 16, 2021 at 6:27pm
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
Jan 17, 2021 at 11:48am
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
Jan 17, 2021 at 11:53am
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
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:04pm
Yes.
Jan 17, 2021 at 12:50pm
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';
}



0 0 0 0 0 0 0 0 0 0


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
Jan 17, 2021 at 2:24pm
Okey, this helps. I think its better to make a loop for array. Thank you so much!
Jan 18, 2021 at 12:48pm
@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.
Topic archived. No new replies allowed.