Different ways to use Const with Reference to a Pointer in C++

Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one:

  • Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting ‘*’ in the declaration.
    datatype *var_name; 
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to
    // demonstrate a Pointer
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Pointer to i
        int* ptr_i = &i;
      
        cout << *ptr_i;
      
        return 0;
    }

    chevron_right

    
    

    Output:



    10
    
  • Reference: When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.
    datatype &var_name; 
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to
    // demonstrate a Reference
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Reference to i.
        int& ref = i;
      
        // Value of i is now
        // changed to 20
        ref = 20;
      
        cout << i;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    20
    
  • References to pointers is a modifiable value that’s used same as a normal pointer.
    datatype *&var_name; 
    

    Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // References to pointers
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Pointer to i
        int* ptr_i = &i;
      
        // Reference to a Pointer ptr_i
        int*& ptr_ref = ptr_i;
      
        cout << *ptr_ref;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    10
    

    Here ptr_ref is a reference to the pointer ptr_i which points to variable ‘i’. Thus printing value at ptr_ref gives the value of ‘i’, which is 10.

    Example 2: Now let us try to change the address represented by a Reference to a Pointer

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // References to pointers
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Reference to a Pointer ptr
        int*& ptr_ref = ptr;
      
        // Trying to change the reference
        // to Pointer ptr_ref to address of j
        ptr_ref = &j;
      
        cout << *ptr;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    5
    

    Here it prints 5, because the value of j is 5 and we changed ptr_ref to point to j. Now as ptr_ref is a reference to pointer ptr, ptr now points to j. Thus we get the output we expected to see.

  • Const Reference to a pointer is a non-modifiable value that’s used same as a const pointer.
    datatype* const &var_name; 
    

    Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // References to pointers
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Const Reference to a Pointer
        int* const& ptr_ref = ptr;
      
        // Trying to change the const reference
        // to Pointer ptr_ref to address of j
        ptr_ref = &j;
      
        cout << *ptr;
      
        return 0;
    }

    chevron_right

    
    

    Compilation Error:



    In function 'int main()':
    prog.cpp:23:13: error: assignment of read-only reference 'ptr_ref'
         ptr_ref = &j;
                 ^
    

    Here we get a compile-time error as it is a const reference to a pointer thus we are not allowed to reassign it.

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // References to pointers
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Const Reference to a Pointer
        int* const& ptr_ref = ptr;
      
        // Trying to change the reference
        // to Pointer ptr_ref
        *ptr_ref = 100;
      
        cout << *ptr;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    100
    

    It prints 100 as it is not a reference to a pointer of a const.

    Why Example 2 didn’t throw a Compile-time error when Example 1 did?

    • In Example 1, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of ptr_ref. So the compiler throws a Compile time error, as we are trying to modify a constant value.
    • In Example 2, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of *ptr_ref, which means we are changing the value of int to which the pointer is pointing, and not the const reference of a pointer. So the compiler doesn’t throw any error and the pointer now points to a value 100. Therefore the int is not a constant here, but the pointer is. As a result, the value of int changed to 100.
  • Reference to a Const Pointer is a reference to a constant pointer.
    datatype const *&var_name; 
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // References to pointers
      
    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Const Pointer to i
        int const* ptr = &i;
      
        // Reference to a Const Pointer
        int const*& ptr_ref = ptr;
      
        // Trying to change the value of the pointer
        // ptr with help of its reference ptr_ref
        *ptr_ref = 124;
      
        cout << *ptr;
        return 0;
    }

    chevron_right

    
    

    Compilation Error:

    In function 'int main()':
    prog.cpp:23:14: error: assignment of read-only location '* ptr_ref'
         *ptr_ref = 124;
                  ^
    

    Here again we get compile time error. This is because here the compiler says to declare ptr_ref as reference to pointer to const int. So we are not allowed to change the value of i.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




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.