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.
Posted Date:- 2021-08-24 04:25:18
Which is the best C++ compiler?
Which operator cannot be overloaded in C++ ?
What differences separate structure from a class in C++?
Can we provide one default constructor for our class?
What is the default constructor?
Is it possible for a C++ program to be compiled without the main() function?
What Is A Unnitialised Pointer?
What Is The Disadvantage Of Using A Macro?
What Do You Mean By Global Variables?
What are logical error and how does it differ from syntax error?
How to run C++ program in cmd?
Explain the significance of vTable and vptr in C++ and how the compiler deals with them
Can we declare variable anywhare in c ?
Control Statements or Control Flow ?
How to download turbo C++ for windows 10?
What is operator overloading in C++?
Can we use a continue statement without using a loop?
What is the difference between the text file and a binary file?
What is the importance of an algorithm in C programming?
Can we have a recursive inline function in C++?
What is the function of the keyword “Volatile�
Briefly explain the concept of Inheritance in C++.
Can we call C++ OOPS? and Why?
What are the User-defined Functions?
What are the Pre-defined Functions?
What are the differences between references and pointers?
What are the differences between C and C++?
What Are Structures And Unions?
Types of errors in c?
Errors in c programming are divid