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

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

Pointers

Updated 2026-05-12
30 min read

Pointers

Pointers are one of the most powerful features in C++, allowing you to directly manipulate memory addresses. Understanding pointers is crucial for low-level programming, dynamic memory management, and efficient data handling. In this tutorial, we'll explore various aspects of pointers, including the address-of operator, dereference operator, pointer declaration, null pointers, void pointers, and pointer arithmetic.

Introduction

In C++, a pointer is a variable that stores the memory address of another variable. Pointers provide direct access to memory locations, enabling you to perform operations like dynamic memory allocation, efficient data structures, and more. Understanding how to use pointers effectively can significantly enhance your programming skills and performance in system-level programming.

Core Content

Address-of Operator (&)

The address-of operator (&) is used to get the memory address of a variable. This operator returns the address where the variable is stored in memory.

address_of.cpp
1#include <iostream>
2
3int main() {
4 int num = 10;
5 std::cout << "Value of num: " << num << std::endl;
6 std::cout << "Address of num: " << &num << std::endl;
7 return 0;
8}
Output
Value of num: 10
Address of num: 0x7ffeeb3f4a2c

Dereference Operator (*)

The dereference operator (*) is used to access the value stored at a memory address. When you use this operator with a pointer, it retrieves the value that the pointer points to.

dereference.cpp
1#include <iostream>
2
3int main() {
4 int num = 10;
5 int* ptr = &num; // Pointer 'ptr' stores the address of 'num'
6 std::cout << "Value of num: " << *ptr << std::endl; // Dereferencing 'ptr' to get the value of 'num'
7 return 0;
8}
Output
Value of num: 10

Pointer Declaration

A pointer is declared by specifying its type followed by an asterisk (*). The type indicates the data type of the variable that the pointer will point to.

pointer_declaration.cpp
1#include <iostream>
2
3int main() {
4 int num = 10;
5 int* ptr; // Declaration of a pointer to an integer
6 ptr = &num; // Assigning the address of 'num' to 'ptr'
7 std::cout << "Value of num: " << *ptr << std::endl;
8 return 0;
9}
Output
Value of num: 10

Null Pointers

A null pointer is a pointer that does not point to any valid memory location. In C++, you can assign nullptr (C++11 and later) or NULL (deprecated) to a pointer to indicate that it points to nothing.

null_pointer.cpp
1#include <iostream>
2
3int main() {
4 int* ptr = nullptr; // Null pointer
5 if (ptr == nullptr) {
6 std::cout << "Pointer is null." << std::endl;
7 }
8 return 0;
9}
Output
Pointer is null.

Void Pointers

A void pointer (void*) is a generic pointer type that can point to any data type. It does not have an associated data type, so you cannot dereference it directly without casting it to the appropriate type.

void_pointer.cpp
1#include <iostream>
2
3int main() {
4 int num = 10;
5 void* ptr = &num; // Void pointer pointing to 'num'
6 int* intPtr = static_cast<int*>(ptr); // Casting void pointer to int pointer
7 std::cout << "Value of num: " << *intPtr << std::endl;
8 return 0;
9}
Output
Value of num: 10

Pointer Arithmetic

Pointer arithmetic involves adding or subtracting integer values to a pointer. This operation changes the address stored in the pointer by moving it forward or backward by a specified number of elements, based on the data type the pointer points to.

pointer_arithmetic.cpp
1#include <iostream>
2
3int main() {
4 int arr[] = {10, 20, 30, 40, 50};
5 int* ptr = arr; // Pointer pointing to the first element of 'arr'
6
7 std::cout << "Value at ptr: " << *ptr << std::endl;
8 ptr++; // Moving pointer to the next element
9 std::cout << "Value at ptr after increment: " << *ptr << std::endl;
10 return 0;
11}
Output
Value at ptr: 10
Value at ptr after increment: 20

Practical Example

Let's create a practical example that demonstrates the use of pointers, including address-of, dereference, and pointer arithmetic.

practical_example.cpp
1#include <iostream>
2
3int main() {
4 int arr[] = {10, 20, 30, 40, 50};
5 int* ptr = arr; // Pointer pointing to the first element of 'arr'
6
7 std::cout << "Array elements using pointer arithmetic:" << std::endl;
8 for (int i = 0; i < 5; i++) {
9 std::cout << "Element at index " << i << ": " << *(ptr + i) << std::endl;
10 }
11 return 0;
12}
Output
Array elements using pointer arithmetic:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

Summary

ConceptDescription
Address-of Operator (&)Retrieves the memory address of a variable.
Dereference Operator (*)Accesses the value stored at a memory address.
Pointer DeclarationDeclares a pointer variable to store addresses.
Null PointersPointers that do not point to any valid memory location.
Void PointersGeneric pointers that can point to any data type, requiring casting for dereferencing.
Pointer ArithmeticOperations on pointers to move them forward or backward by a specified number of elements.

What's Next?

Now that you have a solid understanding of pointers and memory management in C++, the next step is to explore how pointers interact with arrays. In the upcoming tutorial, we will delve into "Pointers and Arrays," where you'll learn how to use pointers to traverse and manipulate array elements efficiently.

Stay tuned for more advanced topics in C++ programming!


PreviousUnionsNext Pointers and Arrays

Recommended Gear

UnionsPointers and Arrays