Pointers and References in C++

  1. Pointers in C++: Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format:
    Syntax:

    datatype *var_name; 
    

    Example:

    // ptr can point to an address
    // which holds int data
    int *ptr;   
    
  2. More on Pointers in C++: This article would provide a further insight into Pointers, on how it works and explains the mathematical background of it. Pointers store address of variables or a memory location.
    // General syntax
    datatype *var_name; 
    
    // An example pointer "ptr" that holds
    // address of an integer variable or holds
    // address of a memory whose value(s) can
    // be accessed as integer values through "ptr"
    int *ptr;
  3. Applications of Pointers in C/C++: A pointer has various applications like:
    • To pass arguments by reference: Passing by reference serves two purposes
    • For accessing array elements: Compiler internally uses pointers to access array elements.
    • To return multiple values: For example in returning square and the square root of numbers.
    • Dynamic memory allocation: We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.
    • To implement data structures.
    • To do system-level programming where memory addresses are useful.
  4. Features and Use of Pointers in C/C++: The Pointers share few important features like it saves memory space, they are used to allocate memory dynamically, it is used for file handling etc. Pointers store address of variables or a memory location.
    Syntax:



    datatype *var_name; 

    Example: pointer “ptr” holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through “ptr”

    int *ptr;
  5. ‘this’ Pointer in C++ The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). Even if only one member of each function exists which is used by multiple objects, the compiler supplies an implicit pointer along with the names of the functions as ‘this’.
    Declaration:

    this->x = x; 
  6. References in C++: When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. There are 3 ways to pass C++ arguments to a function:
    • call-by-value
    • call-by-reference with pointer argument
    • call-by-reference with reference argument
  7. Pointers vs References in C++: This article lays proper ground of differences between Pointer and Reference. Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain.
    Despite the above similarities, there are the following differences between references and pointers.

    • A pointer can be declared as void but a reference can never be void
      Example:

      int a = 10;
      void* aa = &a;. //it is valid
      void &ar = a; // it is not valid
    • References are less powerful than pointers
    • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
    • References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
    • A reference must be initialized when declared. There is no such restriction with pointers
  8. Passing by pointer Vs Passing by Reference in C++: In C++, we can pass parameters to a function either by pointers or by reference. In both the cases, we get the same result. So what should be prefered and why?
  9. Passing Reference to a Pointer in C++: In this artilce let’s compare the usage of a “pointer to pointer” VS “Reference to pointer” in some cases.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.