Difference Between Similar Terms and Objects

Difference between Pointer and Reference

Both pointers and references are the most powerful features in C and C++ which allow programmers to manipulate memory address directly for efficient memory management.

Both are variables that are used to refer to other objects indirectly and they share some common features on the surface. Though they share lots of common capabilities, they are also very complex to understand.

A pointer is a variable that holds the memory address of another variable, whereas a reference is like a pointer that can be used to indirectly refer to another variable.

Most programmers seem to develop some understanding about when to use pointers and when to use references, but they often come across problems the solution of which seems unclear.

So to better understand the philosophy, let’s understand the difference between the two.

What is a Pointer?

Every variable is a memory location which is defined by its address and is paired with an identifier which contains some value that may be changed during the course of a program. In simple terms, variable is a symbol that represents a quantity.

A pointer is a variable that contains the memory address of another variable. A pointer is nothing but an address and a variable that stores a pointer is called a pointer variable.

The idea is to use identifiers (or names) instead of numeric addresses to ease the burden of programming thereby reducing complexity of programs. Since these addresses refer to actual locations in the computer memory, pointers can be used to access and manipulate content stored in the memory.

Like any variable or constant, pointers must be declared for their types. Since pointers contain memory addresses that belong to a separate data type, they must be declared using pointers before you can work with them. The general form of a pointer declaration is:

Data_type * var-name;

Here, “Data_type” is the base type of the pointer and “var-name” is the pointer variable. The asterisk (*) here is used to declare the pointer which designates the variable “var-name” as a pointer variable.

int *p;                        // integer pointer

float *x;                    // float pointer

Here, the variable “p” points to an integer data type which is declared as “int” which refers to the data type of the variable. Similarly, in the second example, “float” refers to the data type of the variable “x” which in fact is designated as a pointer to a floating-point variable.

 

What is a Reference?

A reference is like a pointer with indirect access to a variable. It’s like an alias for another variable, meaning it’s just another name for an already existing variable.

It allows programmers to create a second name for the variable which they can use to manipulate the content stored in that variable. It’s an entity that is an alias for another variable and it has the same address as the original variable. It supports pass-by-reference acting as function formal parameters and once it’s defined to refer to a variable, it cannot be reseated which means it cannot be referred to another variable.

Unlike pointers, they don’t need dereferencing operator to access the value; in fact, they can be used like normal variables.

int i;

int &ri = i;

Here, “ri” is an integer reference initialized to “i”. It declares “ri” as an object of type “reference to int”. A reference can be easily confused with a pointer which is an independent variable, the address values of which cannot be reassigned once created.

 

Difference between Pointer and Reference

  1. Basics of Pointer and Reference

– Both provide almost similar capabilities in terms of usability and the distinction can be tricky. A pointer is just a variable that stores the memory address of another variable, whereas a reference is like an alias for an already existing variable. It creates a second name for a variable that can be used to modify the content stored in that variable.

  1. Declaration of Pointer and Reference

– A pointer is like any other variable which stores a piece of data. Unlike other variables, pointer variables store memory addresses instead of values. A pointer is dereferenced using the indirection operator * to access the memory location of the variable it points to. A reference, on the other hand, doesn’t need any dereferencing operator to access the value.

  1. Reassignment of Pointer and Reference

– Unlike pointers which can be initialized at any time, references must be assigned at initialization. Pointers can be accessed directly, whereas references cannot be directly accessed.

  1. Memory Address for Pointer and Reference

– A pointer variable has its own location in memory, which means when you declare a pointer, you specify a location which is a memory address assigned to each unit of memory. The pointer variable has the same address where it’s saved in memory. A reference variable, on the other hand, is an alias for another variable thereby taking the address of the reference variable.

  1. NULL Value for Pointer and Reference

– NULL represents the end of the list which means it indicates the value is unknown and it specifies that variable does not have a valid address space. NULL value can be assigned to a pointer variable directly which suggests it does not point anywhere, whereas NULL cannot be assigned to a reference variable.

Pointer vs. Reference: Comparison Chart

Summary of Pointer vs. Reference

Pointers have always been among the favorite topics of discussion of C/C++ programmers until references came to the picture. References are somewhat similar to pointers but have their fair share of pros and cons in comparison. While both can be used to change local variables of one function within another function, they have their differences. Both are powerful features in C and C++ which allow you to directly manipulate memory, but with a few exceptions. A pointer holds the memory address of a variable, whereas a reference is just an alias for another variable.

 

Latest posts by Sagar Khillar (see all)

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


1 Comment

  1. Very easy

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Gregoire, Marc. Professional C++. New Jersey: John Wiley & Sons, 2014. Print

[1]Liberty, Jesse and Bradley L. Jones. Teach Yourself C++ in 21 Days. Indianapolis: Sams Publishing, 2004. Print

[2]Reese, Richard M. Understanding and Using C Pointers. Sebastopol: O'Reilly Media, 2013. Print

[3]Image credit: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Pointers.svg/541px-Pointers.svg.png

[4]Image credit: https://commons.wikimedia.org/wiki/File:WeakReference01_2x.png#/media/File:WeakReference01_2x.png

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder