Dereferencing Pointers

The following examples illustrate various scenarios where dereferencing pointers is essential in C++. Understanding how to properly dereference pointers is crucial for accessing and manipulating data stored in memory, whether it be variables, arrays, structures, or functions.

Basic Dereferencing – This example demonstrates the basic concept of dereferencing a pointer to access the value it points to.

#include <iostream>

int main() {
    int x = 10;
    int *ptr = &x; // Pointer initialization with the address of variable x
    std::cout << "Value of x: " << *ptr << std::endl; // Dereferencing ptr to access the value of x
    return 0;
}
  • We declare an integer variable x and initialize it with the value 10.
  • A pointer ptr is declared and initialized with the address of variable x using the address-of operator &.
  • The value of x is accessed and printed to the standard output stream by dereferencing ptr using the asterisk * operator.

Dereferencing Pointer to Array Element

This example demonstrates how to use pointers to access elements of an array using pointer arithmetic and dereferencing.

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr; // Pointer to the first element of the array

    for (int i = 0; i < 5; ++i) {
        std::cout << "Value at position " << i << ": " << *(ptr + i) << std::endl;
    }

    return 0;
}
  • An integer array arr is declared and initialized with five elements.
  • A pointer ptr is declared and initialized with the address of the first element of the array arr.
  • Inside the loop, we use pointer arithmetic to access each element of the array by dereferencing ptr + i.
  • The value of each array element is printed to the standard output stream.

Dereferencing Pointer to Structure Member

This example demonstrates dereferencing a pointer to access members of a structure.

#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point p = {10, 20};
    Point *ptr = &p; // Pointer to the structure

    std::cout << "x coordinate: " << ptr->x << std::endl; // Dereferencing and accessing member using arrow operator
    std::cout << "y coordinate: " << ptr->y << std::endl;

    return 0;
}
  • We define a structure Point with integer members x and y.
  • An instance p of the structure is created and initialized with values 10 and 20.
  • A pointer ptr to the structure is declared and initialized with the address of p.
  • Members x and y of the structure are accessed and printed by dereferencing ptr using the arrow -> operator.

Dereferencing Pointer to Dynamically Allocated Memory

This example demonstrates dereferencing a pointer to access dynamically allocated memory.

#include <iostream>

int main() {
    int *ptr = new int(42); // Dynamically allocate memory for an integer
    std::cout << "Value: " << *ptr << std::endl; // Dereferencing ptr to access the value
    delete ptr; // Deallocate the memory
    return 0;
}
  • Dynamic memory is allocated for an integer using the new operator, and the value 42 is assigned to it.
  • A pointer ptr is declared and initialized with the address of the dynamically allocated memory.
  • The value stored at the memory location pointed to by ptr is accessed and printed to the standard output stream by dereferencing ptr.
  • After use, the dynamically allocated memory is deallocated using the delete operator to avoid memory leaks.

Dereferencing Pointer to Pointer

This example demonstrates dereferencing a pointer to another pointer to access the value it points to.

#include <iostream>

int main() {
    int x = 10;
    int *ptr1 = &x; // Pointer to variable x
    int **ptr2 = &ptr1; // Pointer to pointer

    std::cout << "Value of x: " << **ptr2 << std::endl; // Dereferencing ptr2 twice to access the value of x
    return 0;
}
  • An integer variable x is declared and initialized with the value 10.
  • A pointer ptr1 is declared and initialized with the address of x.
  • A pointer to a pointer ptr2 is declared and initialized with the address of ptr1.
  • To access the value of x, we dereference ptr2 twice using the asterisk * operator.

Dereferencing Pointer to Function

This example demonstrates how to dereference a pointer to a function and invoke the function.

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int main() {
    int (*ptr)(int, int) = add; // Pointer to function
    std::cout << "Result: " << (*ptr)(2, 3) << std::endl; // Dereferencing ptr and invoking the function
    return 0;
}
  • A function add is declared to accept two integer parameters and return their sum.
  • A pointer to a function ptr is declared and initialized with the address of function add.
  • To invoke the function through the pointer, we dereference ptr and pass arguments (2, 3) within parentheses, then print the result.

Dereferencing Pointer to Array of Pointers

This example demonstrates how to dereference a pointer to an array of pointers to access elements pointed to by each pointer.

#include <iostream>

int main() {
    int x = 10, y = 20, z = 30;
    int *ptrArr[] = {&x, &y, &z}; // Array of pointers to integers

    for (int i = 0; i < 3; ++i) {
        std::cout << "Value at index " << i << ": " << *ptrArr[i] << std::endl; // Dereferencing ptrArr[i]
    }

    return 0;
}
  • Three integer variables x, y, and z are declared and initialized with values.
  • An array ptrArr of pointers to integers is declared and initialized with the addresses of variables x, y, and z.
  • Inside the loop, each element of the array is dereferenced using *ptrArr[i] to access the value pointed to by each pointer.

Dereferencing Pointer to Member Variable

This example demonstrates how to dereference a pointer to access a member variable of a structure.

#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point p = {10, 20};
    Point *ptr = &p; // Pointer to the structure

    std::cout << "x coordinate: " << (*ptr).x << std::endl; // Dereferencing ptr and accessing member using dot operator
    std::cout << "y coordinate: " << (*ptr).y << std::endl;

    return 0;
}
  • We define a structure Point with integer members x and y.
  • An instance p of the structure is created and initialized with values 10 and 20.
  • A pointer ptr to the structure is declared and initialized with the address of p.
  • Members x and y of the structure are accessed and printed by dereferencing ptr using the parentheses (*ptr) operator and then accessing the members using the dot . operator.

Dereferencing Pointer to Member Function

This example demonstrates how to dereference a pointer to access and invoke a member function of a class.

#include <iostream>

class MyClass {
public:
    void display() {
        std::cout << "Hello, World!" << std::endl;
    }
};

int main() {
    MyClass obj;
    void (MyClass::*ptr)() = &MyClass::display; // Pointer to member function

    (obj.*ptr)(); // Dereferencing ptr and invoking the member function on obj

    return 0;
}
  • We define a class MyClass with a public member function display.
  • An instance obj of the class is created.
  • A pointer ptr to the member function display within MyClass is declared and initialized with its address.
  • To invoke the member function on obj, we dereference ptr using the parentheses () operator and the object obj using the dot . operator.

Dereferencing Pointer to Array of Structures

This example demonstrates how to dereference a pointer to an array of structures to access members of each structure.

#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point arr[3] = {{1, 2}, {3, 4}, {5, 6}};
    Point (*ptr)[3] = &arr; // Pointer to array of structures

    for (int i = 0; i < 3; ++i) {
        std::cout << "Point " << i + 1 << ": (" << (*ptr)[i].x << ", " << (*ptr)[i].y << ")" << std::endl;
    }

    return 0;
}
  • We define a structure Point with integer members x and y.
  • An array arr of Point structures is declared and initialized with three elements.
  • A pointer ptr to an array of Point structures is declared and initialized with the address of arr.
  • Inside the loop, each element of the array is dereferenced using (*ptr)[i] to access the x and y members of each structure.