-5

I am confused at the output of the following code

#include <iostream>

using namespace std;

int main()

{
int i;
double d;

int r=i;
double s=d;

i=5;
cout<<"value of i:"<<i<<endl;
cout<<"value of i reference:"<<r<<endl;

d=11.7;
cout<<"value of d:"<<d<<endl;
cout<<"value of d reference:"<<s<<endl;

cin.ignore();
return 0;

}

the output is as follows value of i: 5 value of i reference : 4370436 value of d: 11.7 value of d reference: 1.78734e-307

I actually don't know why the value of i reference and d reference are like that. I know if I add "&" to the definitions of i and d references, then it will work as references. But I guess I dont fully understand the meaning of reference. Could anyone explain why in this code the output is like that. Thanks a lot!

2 Answers2

0

There are no references in your code.

r and s have an unspecified value, because i and d (having not been initialised) also did at the point you performed the copy-initialisation. The key being that copy-initialisation is just that: a copy.

For example, r is a new, separate, unrelated integer that — when it was initialised — took on the value of i at that time. That's it! r does not "follow" changes to the value of i.

You seem to at least partially understand that, as your contradictory statement "I know if I add & to the definitions of i and d references, then it will work as references" at least shows that you are aware & is related to references. In fact, having the type int/double rather than the type int&/double& means your objects are not presently references at all.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • A following up question is: why it shows "value of i reference : 4370436"? does "4370436" is just some random number or it has something to do with the "int i"? – jchuworklikeabich Nov 15 '15 at 16:22
  • @jchuworklikeabich: I already said. It's an unspecified value, because you did not initialise `i`. There are plenty of resources that can explain this to you. I strongly advise you to perform some research before asking! – Lightness Races in Orbit Nov 15 '15 at 16:34
0

As @Lightness Races in Orbit has explained the output, you are copying the value of i to r before initialising i. After declaring i in main function, it has garbage value and you are assigning that garbage value to r.

I have edited your code and added references to it to explain references:

#include <iostream>

using namespace std;

int main()

{
int i;

int &r=i;

i=5;
cout<<"value of i:"<<i<<endl;
cout<<"value of i reference:"<<r<<endl;
r = 6; // See this
cout<<"value of i:"<<i<<endl;// Value of i also becomes 6
i = 9;
cout<<"value of i reference:"<<r<<endl;// Value of r also becomes 9

cin.ignore();
return 0;

}

Now output is:

value of i:5
value of i reference:5
value of i:6
value of i reference:9

Here, r is a reference to i. Both r and i point to same variable. It is like having two names for same person. If you do some operations on any of them, changes will be reflected to other also. See the output after changing values of r and i.