What is the difference between reference and pointer?
In programming, specifically in languages like C++, the terms “reference” and “pointer” are used to describe two different ways of manipulating and accessing data.
- Reference: A reference is an alias or an alternative name for an existing object. It provides a way to access the underlying object without directly dealing with memory addresses. In C++, references are created using the 
&symbol. Once a reference is created, it must be initialized with an object, and it cannot be reassigned to refer to another object. When you use a reference, you can access the object directly, and any modifications made to the reference affect the original object. 
Here’s an example that demonstrates the usage of references in C++:
<span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-type">int</span> number = <span class="hljs-number">10</span>;
  <span class="hljs-type">int</span>& ref = number;  <span class="hljs-comment">// 'ref' is a reference to 'number'</span>
ref = <span class="hljs-number">20</span>; <span class="hljs-comment">// Changes 'number' to 20</span>
std::cout << number << std::endl; <span class="hljs-comment">// Output: 20</span>
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
- Pointer: A pointer is a variable that stores the memory address of another object. It allows you to indirectly access and manipulate objects by using the address they reside in. Pointers are created using the 
*symbol. Pointers can be assigned and reassigned to different memory addresses, allowing dynamic memory allocation and deallocation. To access the value of the object pointed to by a pointer, you need to dereference it using the*operator. 
Here’s an example that demonstrates the usage of pointers in C++:
<span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-type">int</span> number = <span class="hljs-number">10</span>;
  <span class="hljs-type">int</span>* ptr = &number;  <span class="hljs-comment">// 'ptr' is a pointer to 'number'</span>
*ptr = <span class="hljs-number">20</span>; <span class="hljs-comment">// Changes 'number' to 20</span>
std::cout << number << std::endl; <span class="hljs-comment">// Output: 20</span>
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
In summary, the main differences between references and pointers are:
- References are aliases for existing objects, while pointers store memory addresses.
 - References cannot be reassigned to refer to a different object, while pointers can be reassigned.
 - References do not require explicit dereferencing, while pointers need to be dereferenced to access the underlying object.
 - Pointers allow dynamic memory allocation and deallocation, while references are limited to aliasing existing objects.
 
Both references and pointers are powerful tools for manipulating and accessing data in C++ and serve different purposes depending on the specific requirements of a program.
