C and C++ are the most commonly used programming languages as these are the basic beginner’s doors to the programming world. Pointers and references are some of the most useful facilities which these languages provide. Pointers are used in C and C++ and references are supported in C++, Python, and other languages.
Pointer vs Reference
The difference between a pointer and a reference is that a pointer stores the address of some other variable in it while a reference refers to an existing variable in some other name. Pointers store the address of any other variable under its memory but references are used as the alternative identity.
A pointer is that variable that has another variable’s address or location as its value. They can be initialized anywhere in a program with operators like ‘*’ and ‘->’. A pointer variable can also be made null and they also support reinitialization. Pointers are declared by base data type followed by an asterisk and pointer’s name.
A reference is that variable that has another name for an already subtle variable of the program. As soon as a reference variable is created, it should be initialized. It makes an alias of another variable. The & operator is used for design references. They are made by base data type followed by ‘&’ and variable’s name.
Comparison Table
Parameters of Comparison | Pointer | Reference |
---|---|---|
Definition | The variable which holds the memory address of another variable is called a pointer variable. | A reference is an alias to another variable, a reference to it. |
Null reference | A null value can be assigned to a pointer. | A reference variable has no null value assignment. |
Syntax | Datatype *PointerName; Where Datatype is the base data type and PointerName is the name of the pointer declared. | Datatype &ReferenceName = AnotherVariable; Where ReferenceName is the name of the reference and AnotherVariable is the variable that is being referred to. |
Initialization | Uninitialized pointers are possible to be created. | References can never be created in an uninitialized |
Reinitialization | Pointer variables can be initialized again and again in the program according to the need. | A reference variable can be initialized only once in a program. |
What is Pointer?
In a nutshell, a pointer is that variable that holds the memory location or address of any other variable in a program. Pointers are functional in C and C++. A pointer variable stores the address location of the pointing variable. Pointer variables can be declared and then, can be initialized with the variable whose address is to be determined.
Moreover, they have clarity like an integer pointer can only hold the address of an integer variable. Pointers have a clear declaration manner. For example, int *point is a pointer to a variable of type int, double *ABC is a pointer to a variable of data type double. The same concept applies to other data types also. Hence, a pointer is declared in the following manner:
int *pointer variable;
After the declaration of the pointer, the pointer variable is assigned to the variable whose address is to be determined. The above declaration statement makes use of a pointer that will store the value of an integer variable.
pointervariable = &a;
Here, a is the integer variable used in the other side of the program whose address is stored in a variable pointer. Pointers also give the liability to get reinitialized. Their values can be changed according to the need.
What is Reference?
A reference variable is an alias for another variable. In other words, the variable that refers to a program’s other subsist variable is considered as a reference variable.
They have a limitation that they cannot be assigned to a null value and they cannot be declared, they are only initialized. After creating a reference, the variable can be accessed by the reference name or by its original name. References are directly initialized. Example,
int &ref = var;
Here, the ref is the name of the reference variable and it points to the variable var which is initialized in another part of the program. When the values of ref and var are printed, ref and var are observed to have the same value. Pointers are advantageous in saving memory because when they are passed as arguments to a function, they use the original memory location for every function call.
Reference variables do not provide reinitialization, these are only once initialized and unique. References do not point to a variable by storing its address in a separate memory location. In simple words, a reference variable is an alternate name of an already existing variable. These are just aliases in the other name of the variable which is to be referenced.
Main Differences Between Pointer and Reference
- A pointer variable is referenced bypass by reference whereas a reference variable is referenced bypass by value.
- Pointers support the facility of pointing to other pointers whereas references lack this advantage. A reference variable cannot be made to point to multiple variables.
- Pointers support the use of arithmetic operators and act as operands whereas reference variables cannot be made compatible with arithmetic operations.
- The size and memory address of a pointer variable is on the stack whereas a reference variable has the original variables’ memory address but it also takes up a bit of space on the stack.
- Pointers have declaration advantages whereas references are only initialized.
References
- https://link.springer.com/article/10.1023/A:1015362608943
- https://dl.acm.org/doi/pdf/10.1145/99583.99599
I am Sandeep Bhandari; I have 20 years of experience in the technology field. I have various technical skills and knowledge in database systems, computer networks, and programming. You can read more about me on my bio page.