What are the differences between references and pointers?

I’m confused between the two. Thanks for any help :grinning:!

7 Likes

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 above similarities, there are following differences between references and pointers.

References are less powerful than pointers

  1. Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
  2. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
  3. A reference must be initialized when declared. There is no such restriction with pointers

Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don’t have above restrictions, and can be used to implement all data structures. References being more powerful in Java, is the main reason Java doesn’t need pointers.

References are safer and easier to use:

  1. Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don’t refer to a valid location (See questions 5 and 6 in the below exercise )
  2. Easier to use: References don’t need dereferencing operator to access the value. They can be used like normal variables. ‘&’ operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator (‘.’), unlike pointers where arrow operator (->) is needed to access members.

Source: https://www.geeksforgeeks.org/commonly-asked-c-interview-questions-set-1/

5 Likes

In C++ Every memory block carries a unique address, which helps the compiler to access it for whatever task the program tells it. Pointers are the variables that store the address of these variables and logically point them.

Pointer Syntax

Data type variable;
Data type *pointer_variable= &variable; // Declaration with Initialisation

Data type variable;
Data type *pointer_variable; // Declaration
pointer_variable= &variable; // Initialisation

A reference creates a variable that acts as an alias for our variable. This means that we can then access our variable either by its original name or by the name which the reference variable offers to us.

Reference syntax

Data type variable;
Data type &reference_variable=variable; // ‘&’ reference operator

Pointers Variables vs Reference Variables

  1. A Pointer can work without initialising whereas a Reference cannot.

  2. A Pointer can point to more than one variable of the same data type, whereas a reference can point to only one variable.

  3. A Pointer can be initialised with NULL, whereas We cannot initialise a Reference with NULL.

Use references when you can, and pointers when you have to.

Refer to this resource for detailed Information