Skip to content

Pointer

Pointers are a powerful feature of many programming languages, particularly C and C++, allowing for efficient management of memory and the implementation of complex data structures.

Definition of a Pointer

A pointer is a variable that stores the memory address of another variable. Pointers provide a way to indirectly access and manipulate the memory location of other variables.

Key Concepts of Pointers

  1. Pointer Declaration: Declaring a pointer involves specifying the data type of the variable it will point to and using an asterisk (*) to denote that it is a pointer.
  2. Pointer Initialization: A pointer must be assigned a valid memory address, often done using the address-of operator (&).
  3. Dereferencing: Accessing the value stored at the memory address a pointer points to, using the dereference operator (*).

Pointer Syntax

Declaration

int *ptr; // Declares a pointer to an integer

Initialization

int var = 10;

int *ptr = &var; // ptr is now holding the address of var

Dereferencing

int value = *ptr; // Accesses the value at the address stored in ptr

Example Usage of Pointers

In C++:

#include <iostream>

using namespace std;

int main() {

    int var = 10; // Declare an integer variable

    int *ptr; // Declare a pointer to an integer

    ptr = &var; // Initialize the pointer with the address of var

    // Output the address stored in ptr

    cout << “Address of var: ” << ptr << endl;

    // Output the value at the address stored in ptr (dereferencing)

    cout << “Value of var through ptr: ” << *ptr << endl;

    // Modify the value at the address stored in ptr

    *ptr = 20;

    // Output the modified value

    cout << “Modified value of var: ” << var << endl;

    return 0;

}

Output:

Address of var: 0x7ffee8b68c38

Value of var through ptr: 10

Modified value of var: 20

Pointer Arithmetic

Pointers can be incremented or decremented to traverse arrays or other contiguous memory blocks. Pointer arithmetic depends on the size of the data type the pointer is pointing to.

#include <iostream>

using namespace std;

int main() {

    int arr[5] = {10, 20, 30, 40, 50};

    int *ptr = arr; // Initialize the pointer to the first element of the array

    for (int i = 0; i < 5; i++) {

        cout << “Value at ptr[” << i << “]: ” << *ptr << endl;

        ptr++; // Move to the next element

    }

    return 0;

}

Output:

Value at ptr[0]: 10

Value at ptr[1]: 20

Value at ptr[2]: 30

Value at ptr[3]: 40

Value at ptr[4]: 50

Pointers and Arrays

Arrays and pointers are closely related. The name of an array acts like a pointer to its first element.

#include <iostream>

using namespace std;

int main() {

    int arr[3] = {1, 2, 3};

    int *ptr = arr; // arr acts like a pointer to the first element of the array

    for (int i = 0; i < 3; i++) {

        cout << “Value at arr[” << i << “]: ” << *(ptr + i) << endl;

    }

    return 0;

}

Output:

Value at arr[0]: 1

Value at arr[1]: 2

Value at arr[2]: 3

Dynamic Memory Allocation

Pointers are essential for dynamic memory allocation, allowing the allocation and deallocation of memory during program execution.

Using new and delete in C++

#include <iostream>

using namespace std;

int main() {

    // Allocate memory dynamically for an integer

    int *ptr = new int;

    *ptr = 10;

    cout << “Value at dynamically allocated memory: ” << *ptr << endl;

    // Deallocate the memory

    delete ptr;

    // Allocate memory dynamically for an array

    int *arr = new int[5];

    for (int i = 0; i < 5; i++) {

        arr[i] = i + 1;

    }

    cout << “Values in dynamically allocated array: “;

    for (int i = 0; i < 5; i++) {

        cout << arr[i] << ” “;

    }

    cout << endl;

    // Deallocate the memory

    delete[] arr;

    return 0;

}

Output:

Value at dynamically allocated memory: 10

Values in dynamically allocated array: 1 2 3 4 5

Pointers to Pointers

Pointers can also point to other pointers, allowing the creation of multi-level pointers.

#include <iostream>

using namespace std;

int main() {

    int var = 10;

    int *ptr = &var;

    int **pptr = &ptr; // Pointer to a pointer

    cout << “Value of var: ” << var << endl;

    cout << “Value of var through ptr: ” << *ptr << endl;

    cout << “Value of var through pptr: ” << **pptr << endl; 

    return 0;

}

Output:

Value of var: 10

Value of var through ptr: 10

Value of var through pptr: 10

Function Pointers

Pointers can also point to functions, allowing dynamic selection and execution of functions.

#include <iostream>

using namespace std;

void greet() {

    cout << “Hello, World!” << endl;

}

int add(int a, int b) {

    return a + b;

}

int main() {

    // Pointer to a function that takes no arguments and returns void

    void (*greetPtr)() = greet;

    greetPtr(); // Call the function through the pointer

    // Pointer to a function that takes two integers and returns an integer

    int (*addPtr)(int, int) = add;

    cout << “Sum: ” << addPtr(5, 3) << endl; // Call the function through the pointer

    return 0;

}

Output:

Hello, World!

Sum: 8

Summary

Pointers are a fundamental aspect of C and C++ programming, providing powerful capabilities for memory management and manipulation. They allow for efficient data structures, dynamic memory allocation, and flexible function handling. Understanding pointers is crucial for mastering low-level programming and working with complex data structures.