Why references are different from pointers?a)A reference cannot be mad...
References and Pointers:
References and pointers are both used in C++ to indirectly access and manipulate variables. However, there are some key differences between them.
A) A reference cannot be made null:
- A reference in C++ must always be initialized with a valid object.
- It cannot be left uninitialized or set to null.
- This ensures that a reference always refers to a valid object, eliminating the possibility of a null reference error.
B) A reference cannot be changed once initialized:
- Once a reference is initialized with an object, it cannot be changed to refer to a different object.
- It remains bound to the same object throughout its lifetime.
- This provides a level of safety, as it prevents accidental reassignment and ensures that the reference consistently refers to the same object.
C) No extra operator is needed for dereferencing of a reference:
- In C++, accessing the value of a reference does not require an extra operator like the * operator used with pointers.
- The reference itself can be treated as if it were the object it refers to.
- This simplifies the syntax and makes code more readable.
Summary:
In summary, references and pointers serve similar purposes in C++, but there are some important distinctions between them. References cannot be null, they cannot be changed once initialized, and they do not require an extra operator for dereferencing. These characteristics make references safer and more convenient to use in certain situations. However, pointers still have their own advantages, such as the ability to be null and to be reassigned to different objects. Therefore, the choice between references and pointers depends on the specific requirements of the program and the desired behavior.
Why references are different from pointers?a)A reference cannot be mad...
References cannot be made null whereas a pointer can be. References cannot be changed whereas pointers can be modified.
Pointers need * operator to dereference the value present inside it whereas reference does not need an operator for dereferencing.
To make sure you are not studying endlessly, EduRev has designed Class 7 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 7.