+ 16

What are the differences between references and pointers?

I'm confused between them.

5th Aug 2019, 6:04 PM
Bob Brown
Bob Brown - avatar
10 ответов
+ 20
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 Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing. 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: 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 ) 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.
5th Aug 2019, 6:06 PM
Green Ghost
Green Ghost - avatar
+ 9
References are better 😏
5th Aug 2019, 6:15 PM
ᗰᗩᔕ丅ᗴᖇ
ᗰᗩᔕ丅ᗴᖇ - avatar
+ 6
References use pointers behind the scenes.
6th Aug 2019, 10:50 PM
Sonic
Sonic - avatar
+ 6
Basically, a pointer points to a certain location in memory (either in the heap or local). If you dereference that pointer, you get the data to which it's pointing and you can cast the pointer to other data types (if memory serves) (hah). A reference if basically saving you the trouble of having to dereference to get to the value, but also ensures the value you set is changing what was passed. When you pass a variable normally, it's by value (a copy of the data). References let you change the variable that was passed into the calling function to the result to which it was set in the function. You can use a pointer to do this as well, however. It depends on what you plan to do with the variable.
7th Aug 2019, 4:12 AM
Jen
+ 4
Both of them are used to reference or use original variable . only difference between them is that pointer variable stores address of original variable and therefore occupy memory of 4 byte (in 64 bit system) whereas reference is alias (another name) to original variable and the share same memory locations. And last difference is of their syntax of declaration and usage.
6th Aug 2019, 2:31 AM
Devarsh Mavani
Devarsh Mavani - avatar
+ 2
Point there is something that the is looking to a address and your reference is the same address I mean the reference is the address am self just with the other name of a parable
5th Aug 2019, 8:37 PM
avi Rosenberg
avi Rosenberg  - avatar
+ 2
Essentially the same thing ! A pointer cannot be created without a reference and a reference implicitly implements a pointer 🤔
6th Aug 2019, 1:40 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
References are constant pointers.
6th Aug 2019, 2:53 PM
nidhish
nidhish - avatar
+ 2
A reference is basically a pointer. The only functional difference is that references can't be null under normal circumstances. Aside from some minor syntax differences (such as using . vs -> for member access), they're effectively the same thing.
6th Aug 2019, 8:12 PM
Nemo
+ 1
References are used to give a new name or an alias to the same variable and pointers are variable which can store the address of variable. Then it can also be used to alter the value of variable.
6th Aug 2019, 6:07 AM
Ashish Bansal
Ashish Bansal - avatar