Programming with Passion

Make the best out of everything.

Friday, 8 April 2016

What are the difference between reference variables and pointers in C++?

What are the difference between reference variables and pointers in C++?
[This question is usually asked in a twisted way during c++ interviews. Sometimes the interviewer might use examples and ask you to find the error.]
PointersReference Variables
Pointers can be assigned to NULLReferences cannot be assigned NULL. It should always be associated with actual memory, not NULL.
Pointers can be (re)pointed to any object, at any time, any number of times during the execution.Reference variables should be initialized with an object when they are created and they cannot be reinitialized to refer to another object
Pointer has own memory address and location on stackReference variables has location on stack, but shares the same memory location with the object it refer to.

No comments:

Post a Comment