Compound Types

I've ran the following program:

#include <iostream>
int main()
{
int aval = 6;
int valu = 3;
int &six = aval;
int &three = valu;
--aval;
std::cout << &six << " " << &three;
return 0;

}

And the output was 0x28fef4 0x28fef0

I was expecting to see 6 3

What is causing this output?

Using Codeblocks.
Last edited on
You are outputing addresses of references. Instead of

std::cout << &six << " " << &three;

use

std::cout << six << " " << three;

to get the desired result.
Last edited on
You declare two ints, aval and valu
You then declare two references to integers. The references, "six" and "three" refer to the integers "aval" and "valu" respectively. A reference is just an "alias", which means that any changes to the reference also change the data it refers to.

Then you try to do output of "&six". The ampersand is the "address-of" operator, which means when you do this...

cout << &six

You are not printing the referenced data (aval), but the address-of the reference. Hence why you have an address printed.
Last edited on
Finally got it. If it was described in the book (C++ Primer) the same way you described it, I would'nt have had a problem.

Thank's for the replies :)
Topic archived. No new replies allowed.