codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

34 / 87 topics
33Pointers34Pointers and Arrays35References36Call by Reference (Using Pointers)37Memory Management: new and delete
Tutorials/C++ Programming/Pointers and Arrays
⚡C++ Programming

Pointers and Arrays

Updated 2026-04-20
3 min read

Introduction

Pointers and arrays are fundamental concepts in C++ programming, playing a crucial role in memory management and efficient data handling. Understanding how pointers interact with arrays is essential for mastering the language and writing high-performance applications.

In this tutorial, we will explore:

  • What pointers are and how they relate to arrays.
  • How to declare, initialize, and use pointers with arrays.
  • Pointer arithmetic and its implications on array manipulation.
  • Best practices for using pointers and arrays in C++.

What Are Pointers?

A pointer is a variable that stores the memory address of another variable. In C++, pointers are declared by specifying the data type they point to followed by an asterisk (*). For example:

int *ptr;

This declares ptr as a pointer to an integer.

Pointers and Arrays

In C++, arrays are contiguous blocks of memory, where each element is accessed via its index. Pointers can be used to represent the base address of an array, allowing for flexible manipulation of array elements.

Declaring and Initializing Pointers with Arrays

When you declare an array, its name acts as a pointer to the first element of the array. For example:

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

Here, arr is a pointer to the first integer in the array.

To declare a pointer that points to an array, you can do the following:

int *ptr = arr;

This sets ptr to point to the first element of arr.

Accessing Array Elements Using Pointers

Pointers can be used to access and modify array elements. The expression *(ptr + i) is equivalent to arr[i]. For example:

int value = *(ptr + 2); // value will be 30

This retrieves the third element of the array.

Pointer Arithmetic

Pointer arithmetic allows you to navigate through array elements by adding or subtracting integer values from a pointer. This is based on the size of the data type the pointer points to.

ptr++; // Moves ptr to the next integer in the array

This increments ptr to point to the second element of the array.

Example: Using Pointers with Arrays

Here's a complete example demonstrating how to use pointers with arrays:

#include <iostream>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;

    // Accessing elements using pointer arithmetic
    for (int i = 0; i < 5; i++) {
        std::cout << "Element at index " << i << ": " << *(ptr + i) << std::endl;
    }

    // Modifying elements using pointers
    *(ptr + 2) = 35; // Changes the third element to 35

    // Printing modified array
    for (int i = 0; i < 5; i++) {
        std::cout << "Modified element at index " << i << ": " << arr[i] << std::endl;
    }

    return 0;
}

Best Practices

  1. Always Initialize Pointers: Uninitialized pointers can lead to undefined behavior.
  2. Avoid Dangling Pointers: Ensure that a pointer is not left pointing to deallocated memory.
  3. Use nullptr for Null Pointers: Prefer using nullptr instead of NULL or 0 for null pointers in C++11 and later.
  4. Bounds Checking: Always ensure that your pointer operations do not go out of bounds, which can cause buffer overflows.

Conclusion

Pointers and arrays are powerful tools in C++ programming, enabling efficient memory management and flexible data manipulation. By understanding how pointers interact with arrays, you can write more effective and optimized code. Remember to follow best practices to avoid common pitfalls such as uninitialized or dangling pointers.

In the next section, we will delve deeper into dynamic memory allocation using new and delete, further enhancing your ability to manage memory in C++.


PreviousPointersNext References

Recommended Gear

PointersReferences