pointer vs refrences

Q->what is the basic difference between pointer and refrence variable....?

if there is any difference then show it with the help of example....

if no then why ?

as per as I know there is no difference between pointer and refrence variable...


closed account (o3hC5Di1)
Hi there,

In usage, the main difference between the two is that once a reference variable is initialized, it's value (i.e. the value it points to in memory) cannot be changed. Also, you do not need to dereference references as you need to do with pointers.

All the best,
NwN
Last edited on
a pointer is an object: it has a size, an address, an identity
a reference variable is not an object: has no size, no address, etc -- it's just another name for some other object
@Jai

A reference must refer to a variable, although this becomes false when it goes out of scope, as in returning a local reference from a function (a bad practice). A pointer on the other hand can be made to point at anything including garbage or NULL. For this reason references are better.

One can't do math with references, but you can with pointers - for example in addressing array elements, like in C.

They are very similar, but the implementation of a reference by a compiler apparently doesn't have to be done with pointers, although I am not sure how else it would be done.

Hope this helps a bit, there are plenty of others who know much more than me.
Topic archived. No new replies allowed.