+ 16
What are the differences between references and pointers?
I'm confused between them.
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.
+ 9
References are better 😏
+ 6
References use pointers behind the scenes.
+ 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.
+ 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.
+ 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
+ 2
Essentially the same thing ! A pointer cannot be created without a reference and a reference implicitly implements a pointer 🤔
+ 2
References are constant pointers.
+ 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.
+ 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.