» Quick Introduction to C++ » 1. Basics » 1.6 Pointers

Pointers

A pointer is a variable that can hold the address of another variable.

int* a; // `a` is a pointer to type int
double *ptrPi; // `ptrPi` is a pointer to type double

Address Resolution

The address-of operator & is used to obtain the memory address of a variable.

#include <iostream>

int main() {
    // Declare a variable
    int number = 42;

    // Print the value and address of the variable
    std::cout << "Value of number: " << number << std::endl;
    std::cout << "Address of number: " << &number << std::endl;

    return 0;
}

Dereference

The dereference operator * is used to access the value stored at a particular memory address pointed to by a pointer.

For example:

#include <iostream>

int main() {
    // Declare a variable
    int number = 58;

    // Get the memory address of the variable using the address-of operator
    int* pointerToNumber = &number;

    // Print the value and address using the pointer
    std::cout << "Value through pointer: " << *pointerToNumber << std::endl;
    std::cout << "Address stored in pointer: " << pointerToNumber << std::endl;

    return 0;
}

The dereference operator allows you to manipulate the values at the memory locations pointed to by pointers. It is an essential concept when working with pointers in C++.

Pointer Arithmetic

Pointer arithmetic in C++ involves performing arithmetic operations on pointers to navigate through the memory locations they point to. This is particularly common when working with arrays or dynamically allocated memory.

#include <iostream>

int main() {
    // Create an array of integers
    int numbers[] = {10, 20, 30, 40, 50};

    // Declare a pointer to the first element of the array
    int* ptr = numbers;

    // Accessing elements using pointer arithmetic
    std::cout << "Element at index 0: " << *ptr << std::endl;
    std::cout << "Element at index 1: " << *(ptr + 1) << std::endl;
    std::cout << "Element at index 2: " << *(ptr + 2) << std::endl;

    // Move the pointer to the next element
    ptr++;

    // Accessing the next element using the updated pointer
    std::cout << "Element at index 1 (after pointer increment): " << *ptr << std::endl;

    // Move the pointer back to the previous element
    ptr--;

    // Accessing the previous element using the updated pointer
    std::cout << "Element at index 0 (after pointer decrement): " << *ptr << std::endl;

    // Pointer arithmetic with indices
    int index = 2;
    std::cout << "Element at index " << index << ": " << *(ptr + index) << std::endl;

    return 0;
}

Heap Allocation

Heap allocation in C++ refers to dynamically allocating memory from the heap, which is the area of memory managed by the programmer. This is in contrast to the stack, which is managed automatically and has a limited size. Dynamic memory allocation is achieved using the new operator to allocate memory and the delete operator to free that memory.

#include <iostream>

int main() {
    // Dynamically allocate an integer on the heap
    int* dynamicInt = new int;

    // Assign a value to the dynamically allocated integer
    *dynamicInt = 42;

    std::cout << "Value stored in dynamicInt: " << *dynamicInt << std::endl;

    // Free the dynamically allocated memory
    delete dynamicInt;

    return 0;
}

It's important to note that for every new, there should be a corresponding delete to avoid memory leaks. Failing to delete allocated memory can lead to a situation where the program consumes more and more memory without releasing it.

#include <iostream>

int main() {
    // Dynamically allocate an array of integers on the heap
    int* dynamicArray = new int[5];

    for (int i = 0; i < 5; ++i) {
        dynamicArray[i] = i * 10;
    }
    for (int i = 0; i < 5; ++i) {
        std::cout << "Element at index " << i << ": " << dynamicArray[i] << std::endl;
    }

    // Free the dynamically allocated array
    delete[] dynamicArray;

    return 0;
}

Pass by Reference

For readability and null reference safety, C++ introduced “passing by reference” as well. It involves passing the memory address (reference) of a variable to a function instead of the variable's value. This allows the function to directly operate on the original variable, making it more efficient than passing by value. The syntax for passing by reference uses the & symbol.

#include <iostream>

// Function that takes an integer reference
void incrementByReference(int &num) {
    num++;
}

int main() {
    int value = 5;

    std::cout << "Original value: " << value << std::endl;

    // Pass the variable 'value' by reference to the function
    incrementByReference(value);

    std::cout << "Value after incrementing by reference: " << value << std::endl;

    return 0;
}

Code Challenge

Write a C++ program that uses pointers to swap the values of two integers.

Loading...
> code result goes here
Prev
Next