Monday 8 February 2016

Reference Variables in C++

We have seen types of variables while studying C and C++ and we know that In C++ we have 3 different types of variables:
  • Ordinary Variable // int x;
  • Pointer Variable   // int *x;
  • Reference Variable  // int &y = x;
Here we are going to study about the reference variable and how it is different from other types of variables and why do we need the reference variables when we already have pointers?.

Okay, First have a look at the syntax of reference variable :
int &y = x;
  • Here the symbol '&' will not be called as the 'address of' operator.
  • It will be called reference operator.
  • We would have called '&' as the address of operator, if the '&' will be at right side of ' = ' (assignment operator).
Some time I also call the reference variable as the internal pointer because it works exactly (not all time) the same as the pointer variable.

Some Important points:

  • It is important to initialize the reference variable while its declaration.
  • And the initialization should be done with a variable which is already present in the memory of program.
  • eg.
  • if we write this :
  • int &y = x;
  • Then 'x' has to be already declared into the program (i.e  there has to be a memory allocated for x)
  • 'y' is just another name of x.
  • For understanding purpose we can say that there are two name of same block in the memory of the program. (Note that this statement is illusion,  the truth is that u is also a seperate variable which has the reference of x [i.e address of x])
  • Reference variable is initialized with already declared variables only.
  • Reference variables can't be updated.
Why do we need reference variable?
Answer : We use the reference variable for operator overloading. Except operator overloading there are no task which can't be accomplished with pointers. Even sometime operator overloading will also not have any problem with pointer variables as the parameters. But there are cases when using pointers for "passing via reference" would give you unacceptable syntax.
There is a link here. Here you will find a answer given by Strousturup about the inclusion of reference variable in C++ program.
For more programming knowledge you can visit here.

4 comments:

  1. https://www.hackerearth.com/problem/algorithm/little-jhool-and-brute-force-18/

    ReplyDelete
  2. #include
    #include
    int main()
    {
    std::string str[100];

    int i =0;
    do
    {
    i++;
    std::cin>>str[i];
    }while(str[i]!='\n');
    int j,k;
    for(j=0,k=i-1; j<i/2; j++, k--)
    {
    std::string temp = str[j];
    str[j] = str[k];
    str[k] = temp;
    }
    for(j = 1; j<=i; j++)
    std::cout<<str[j]<<" ";
    return 0;
    }

    ReplyDelete
  3. #include
    int main()
    {
    std::string str;
    std::getline(std::cin, str);
    std::size_t cnt = std::count(str.begin(), str.end(), ' ');
    for(int i = 0, j = str.length()-1; i < str.length()/2; i++, j--)
    {
    char temp = str[i];
    str[i] = str[j];
    str[j] = temp;
    }
    int count[100];
    int z = 0;
    for(int i =0; i < str.length(); i++)
    {
    int k = 0;
    while(str[i] != ' ')
    {
    k++;
    i++;
    }
    count[z] = k;
    z++;

    }
    for(int i = 0; i < cnt; i++)
    {
    for(int j = 0, k = count[j]-1 ; str[j] != ' '; j++, k--)
    {
    char temp = str[j];
    str[j] = str[k];
    str[k] = temp;
    }
    }
    std::cout<<str<<std::endl;
    }

    ReplyDelete