C++ Pointers


In C++, pointers are variables that store the memory address of another variable. Pointers are a powerful feature of C++ that allow for dynamic memory management, efficient array manipulation, function arguments, and more.

Basics of Pointers

A pointer stores the address of a variable rather than the value itself. Here are some important concepts about pointers:

  • Pointer declaration: A pointer is declared using the * symbol.
  • Address-of operator (&): This operator returns the memory address of a variable.
  • Dereference operator (*): This operator allows you to access the value stored at the pointer's memory address.

Pointer Declaration

int* ptr; // Declares a pointer to an integer
  • int* ptr; declares a pointer variable ptr that can store the address of an integer.

Example of Pointer Usage

int x = 10; // Declare an integer variable int* ptr = &x; // Declare a pointer and assign it the address of x std::cout << "x: " << x << std::endl; // Prints the value of x std::cout << "Address of x: " << &x << std::endl; // Prints the address of x std::cout << "ptr: " << ptr << std::endl; // Prints the value of ptr (address of x) std::cout << "Value at ptr: " << *ptr << std::endl; // Prints the value stored at the address ptr points to (i.e., x)

Explanation:

  • &x returns the memory address of x.
  • ptr stores the address of x.
  • *ptr dereferences the pointer and accesses the value stored at the address it points to (in this case, the value of x, which is 10).

Key Concepts

  1. Pointer Declaration: You declare a pointer using the following syntax:

    type* pointerName;

    For example

    int* p; // p is a pointer to an integer
  2. Pointer Initialization: A pointer can be initialized to point to the address of a variable:

    int a = 10; int* p = &a; // p now holds the address of variable a
  3. Dereferencing a Pointer: Dereferencing a pointer means accessing the value at the memory address stored by the pointer. This is done using the * operator:

    std::cout << *p; // Outputs the value of a (which is 10)
  4. Null Pointers: A pointer can be set to nullptr (or NULL in older code), which means it doesn't point to any valid memory location:

    int* p = nullptr;
  5. Pointer Arithmetic: Pointers can be incremented or decremented. When you increment a pointer, it moves to the next memory location based on the type it points to:

    int arr[3] = {10, 20, 30}; int* p = arr; // Points to the first element of arr p++; // Now points to the second element of arr (p+1) std::cout << *p; // Outputs 20

Example: Pointers with Arrays

Pointers and arrays are closely related in C++. The name of an array is a pointer to the first element of the array.

int arr[3] = {10, 20, 30}; int* p = arr; // arr is a pointer to the first element of the array std::cout << *p << std::endl; // Outputs 10 (first element) std::cout << *(p + 1) << std::endl; // Outputs 20 (second element) std::cout << *(p + 2) << std::endl; // Outputs 30 (third element)

Dynamic Memory Allocation with Pointers

Pointers allow you to allocate memory dynamically during the program's runtime using new and deallocate it using delete.

int* p = new int; // Allocate memory for an integer *p = 42; // Assign a value to the allocated memory std::cout << *p; // Outputs 42 delete p; // Free the allocated memory

You can also allocate memory for arrays dynamically:

int* arr = new int[5]; // Allocate memory for an array of 5 integers for (int i = 0; i < 5; i++) { arr[i] = i * 2; // Assign values } delete[] arr; // Free the memory for the array

Pointers to Functions

In C++, pointers can also point to functions, allowing you to pass functions as arguments to other functions.

int add(int a, int b) { return a + b; } int (*funcPtr)(int, int) = &add; // Pointer to a function that takes two int and returns int std::cout << funcPtr(2, 3); // Outputs 5

Summary

  • Pointers store the memory address of other variables.
  • The address-of operator (&) gives the memory address of a variable.
  • The dereference operator (*) accesses the value stored at a pointer’s address.
  • Pointers enable powerful techniques like dynamic memory allocation and array manipulation.