Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Reference and Pointers in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 670
    Comment on it

    REFERENCE---  A reference variable is initialized first. A reference variable is defined to indicate a variable and that reference variable can't point to the another variable. References can not be NULL.

    e.g of reference variable is:

     

    #include <iostream>
     using namespace std;
     int main ()
     {   // declare simple variables 
      int a; 
      double d;
    
       // declare reference variables 
      int& r = a;
      double& s = d;
    
      a = 5;
      cout << "Value of a : " << a << endl;
      cout << "Value of a reference : " << r << endl;
      d = 11.7; 
      cout << "Value of d : " << d << endl;
      cout << "Value of d reference : " << s << endl; 
    
      return 0;
      }

     

    output of above code is:

    Value of i : 5
    Value of i reference : 5
    Value of d : 11.7
    Value of d reference : 11.7

     

    POINTERS-- With the help of pointers some tasks are performed more easily, some tasks can not be performed without pointers(dynamic memory allocation). A pointer is a variable that holds the memory address of another object.

    Pointers are powerful because it allows us to access address and manipulate the data. If it is correctly used , it increases the performance and efficiency.

    syntax of declaring pointer is:

    type *ptr;

    e.g of pointer:

    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
       int  var1;
       char var2[10];
    
       cout << "Address of var1 variable: ";
       cout << &var1 << endl;
    
       cout << "Address of var2 variable: ";
       cout << &var2 << endl;
    
       return 0;
    }

     

    output of above program is:

    Address of var1 variable: 0xbfebd5c0
    Address of var2 variable: 0xbfebd5b6

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: