Switch to full style
For C/C++ coders discussions and solutions
Post a reply

References confuse me. How do they differ from pointers?

Wed Jul 04, 2007 12:44 am

References confuse me. How do they differ from pointers?
----------------------------------------------------------------------------------

A reference is an alias to a variable, object. They are merely alternate identifiers for the same object. Note that a variable and its reference can be used interchangeably. Hence they are synonyms of each other...Although they contain the address of the object, however they are more like Constant Pointers which cannot be altered.

Regular pointers can be manipulated, i.e. incremented; decremented which can also be dangerous. However a reference cannot be manipulated. Hence they are safer to use because you are not accidentally changing the address of the variable .

cpp code
int actualint;
int& otherint = actaulint;


The real usefulness of references is when they are used to pass values into functions. They provide a way to return values from the function.The reference lets you pass and return large data structures without the overhead of copying them. A reference is also a way to avoid pointer dereferencing syntax in your code. The & operator identifies a reference variable.



Post a reply
  Related Posts  to : References confuse me. How do they differ from pointers?
 How does JSP differ from Servlets?!!!     -  
 Constant References     -  
 Functions and References     -  
 Basic Pointers     -  
 Swap Using Pointers     -  
 Arrays using Pointers     -  
 Types of Pointers in C++     -  
 How to use pointers and what it means     -  
 The using of pointers between two variables (Swaping)     -  
 pointers to derived types     -  

Topic Tags

C++ Basics