-5

I have declared two variables in following ways:

void main()
{
  int a = 20;
  int &b = a;
  cout<<a<<" "<<b;
}

Output: 20 20

Kindly advise what exactly happens when i write int &b = a and how is it different from the first declaration and also when to use which.

Krishna
  • 35
  • 6

3 Answers3

3

Kindly advise what exactly happens when i write int &b = a and how is it different from the first declaration

In the example below:

int a;
int & b = a;

The b variable refers to the memory address of the a variable. The variable b is just an alias for a.

and also when to use which.

A common usage of references is to avoid copying large objects. Rather than passing (copying) the large object, you pass a reference to it.

const std::string& Get_Tree_Name()
{
  static std::string name = "Eucalyptus";
  return name;
}

The above function returns a reference to a static string inside the function. So rather than copying the string after the function exits, it is passing a reference. In general, a reference would occupy less space than the std::string object.

Another usage is when a function modifies its parameter:

void Get_Pi(double& pi_value)
{
  pi_value = 3.14159264;
}

int main()
{
  double my_pi = 0.0;
  Get_Pi(my_pi);
}

In the main function of the above example, the code in the Get_Pi function is actually modifying the my_pi variable in the main function, because of the reference mechanism.

Without the references, the Get_Pi function would modify a copy of the my_pi variable and not the original variable, since parameters are passed by copy.

Pointers vs. references

Another alternative for a function to modify its parameter is to pass the parameter by pointer. One issue with using a pointer is that pointer can point to anything and not necessarily a variable.

Passing a reference means that the object must exist. A reference, unlike a pointer, doesn't need to be validated during run-time. The compiler performs the checking. This is important in safety critical systems.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
-2
Int a = 0;

This a simple variable of int which can store only -32768 to 32768 (2 bytes)

Int * b; 

This also a variable, the * means that this vatiable can store address of some another variable not value.

b=&a;

Now this how you put value in a pointer, writing & before a variable name will give you an address of the variable instead of variable value.

*b;

This how can get value from a pointer, this will give you a value of a

  • 1
    "-32768 to 32768 (2 bytes)" Not true. `int`s size is set by the compiler inmplementer. `int`'s minimum size is 16 bits (2 bytes). The upper size is unbounded, but there aren't many implementations that go over 64 bit (8 bytes). Here is a page on data type sizes and ranges : http://en.cppreference.com/w/cpp/language/types – user4581301 Sep 14 '17 at 18:51
  • 1
    The rest of this answer is discussing pointers. The question does not ask about pointers. It is asking about references. – user4581301 Sep 14 '17 at 18:53
  • I think 20 ?? Im i Right?? – Umer Waqas - Top rated dev Sep 14 '17 at 18:57
  • Covered in the link in the first comment. – user4581301 Sep 14 '17 at 18:59
-3

In first variable you are assigning value to the variable a (a=20) In second line you create a variable and give that variable address of a. Now b is also pointing on the adress of a. Now a and b are pointing to the same memory adress which is 20.

Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
  • Pointers may or may not be involved when dealing with references. The compiler is well within it's rights to simply use two names for the same memory location. – user4581301 Sep 14 '17 at 18:56
  • And &b=a means address of b is equal to a. Means now b is also pointing a – Sohaib Anwaar Sep 14 '17 at 19:00
  • You are not necessarily storing an address. `&b=a` means `b` is an alias for `a`. The compiler could quietly go through the code and replace all of the `b`s with `a`s when generating the output. – user4581301 Sep 14 '17 at 19:04