C++ Dereferencing



In C++, dereferencing is the process that helps in accessing the value that a pointer points to. Where pointers store the memory address of that particular value. To access or modify the value stored at that address, you can use the dereference operator denoted by (*).

Dereferencing to Read a Value

Here is the syntax to access the value, which is stored at the address the pointer points to −

 Type value = *pointer; // Gets the value at the address pointed to by the pointer 

Dereferencing to Modify a Value

Syntax of dereference operator to modify the value at the address the pointer points to −

 *pointer = newValue; // Sets the value at the address pointed to by the pointer 

Example of C++ Dereferencing

Heres a simple example illustrating dereferencing in C++ −

 #include <iostream> using namespace std; int main() { int number = 42; // A normal integer variable int* ptr = &number; // Pointer that holds the address of 'number' // Dereferencing to read the value cout << "Original Value: " << *ptr << endl; // Dereferencing to modify the value *ptr = 100; cout << "Modified Value: " << number << endl; return 0; } 

Output

 Original Value: 42 Modified Value: 100 

Explanation

  • Firstly, we declared an integer variable named number and initialized it with the value 42.
  • Declared a pointer ptr of data type integer. The pointer is assigned the address of the number using the address-of operator &. which means ptr will point to the location of the memory address of a number.
  • Now, we used the dereference operator * to access the value stored at the address pointed to by ptr.
  • *ptr = 100; This line uses the dereference operator again, but this time to assign a new value.

References vs Dereference

ReferencesDereference
DefinitionReference is a variable for another variable, which allows you to access or modify the original variable without using its name directly.Dereferencing is the process of accessing the value stored at the memory address held by a pointer.
Symbol& (used in declaration)* (used in expression)
SyntaxdataType& referenceName = existingVariable;dataType pointerVariable = *pointerName;

Example Showing both Reference and Dereference

Heres an example illustrating both reference and dereference in a code.

 #include <iostream> using namespace std; int main() { int number = 42; // Pointer holding the address of 'number' int* ptr = &number; // Reference to 'number' int& ref = number; // Using pointer to read the value (dereferencing ptr) cout << "Original Value (pointer): " << *ptr << endl; // Using reference to read the value cout << "Original Value (reference): " << ref << endl; // Modifying the value through the pointer (dereferencing ptr) *ptr = 100; cout << "Modified Value (pointer): " << number << endl; cout << "Modified Value (reference): " << ref << endl; // Modifying the value through the reference ref = 200; cout << "Modified Value (reference): " << number << endl; cout << "Modified Value (pointer): " << *ptr << endl; return 0; } 

Output

 Original Value (pointer): 42 Original Value (reference): 42 Modified Value (pointer): 100 Modified Value (reference): 100 Modified Value (reference): 200 Modified Value (pointer): 200 
Advertisements
close