Differences Between Pointer and Reference in C++ are as follows,

1.) Reference is a kind of const pointer that de-reference itself automatically. Yes, its similar to const pointer therefore once you have attached a reference to a variable or object then you cannot make it to point to someone else.

Now if you try to point the already initialized reference to an another variable then reference will not change its reference point, this kind of assignment will only change the value of variable to which reference was pointing.

Where as, you can change the value of a pointer i.e. at run time you can force a pointer to point to new memory location.

Let’s understand by example,

2.)  Its must to initialize a reference while declaration i.e. Where as, you can declare a pointer without initializing it. Therefore, you can also assign a NULL to pointer. Where as, its not possible with a Reference.

3.)  You cann’t have reference of reference, where as you can have pointer to pointer.

4.)  You can increment and decrement the value of a pointer and can use it for random indexing i.e.

But it’s not possible with reference.

5.)  If you take the address of a reference then it will be similar to the address of cariable to which it is pointing. Where as, in case of pointer it is different.

6.)  With pointers there is always a risk and extra effort to delete it if allocated on heap. There can be problems like memory leak and dangling pointers. Whereas, in case of references it’s cool i.e. you don’t need to worry about problems like these.




Click Here to Subscribe for more Articles / Tutorials like this.