C++ / C++ Pointers and Memory Management

Performing Pointer Arithmetic in C++

In this tutorial, you'll learn about pointer arithmetic in C++. This involves understanding how to perform arithmetic operations on pointers.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers pointer concepts, dynamic memory allocation, and memory management in C++.

Introduction

This tutorial aims to provide an in-depth understanding of pointer arithmetic in C++. You will learn how to perform arithmetic operations on pointers and how to use them effectively in your code.

By the end of this tutorial, you will be able to:
- Understand what pointer arithmetic is
- Know how to use addition and subtraction operations on pointers
- Understand the difference between incrementing a pointer and incrementing the value pointed by a pointer

Prerequisites:
- Basic knowledge of C++ programming
- Understanding of pointers in C++

Step-by-Step Guide

In C++, we can perform arithmetic operations on pointers. These operations include addition, subtraction, increment, and decrement. However, multiplication and division operations are not allowed on pointers.

Here are some important points about pointer arithmetic:

  • When we increment a pointer, the pointer is moved to the next memory location.
  • When we decrement a pointer, the pointer is moved to the previous memory location.
  • The memory location a pointer points to depends on the data type of the pointer. For example, if we have an integer pointer, incrementing this pointer will move it to the next integer in memory (typically 4 bytes ahead).

Code Examples

Let's take a look at some examples:

Example 1: Incrementing a Pointer

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; // pointer p points to the first element of the array

cout << *p << endl; // 10

p++; // increment the pointer

cout << *p << endl; // 20

In this example, we first declare an integer array and a pointer that points to the first element of the array. Then, we increment the pointer using p++. After incrementing, the pointer p points to the next element in the array.

Example 2: Decrementing a Pointer

int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[4]; // pointer p points to the last element of the array

cout << *p << endl; // 50

p--; // decrement the pointer

cout << *p << endl; // 40

In this example, the pointer p initially points to the last element of the array. After we decrement the pointer using p--, it points to the previous element in the array.

Summary

In this tutorial, we've learned about pointer arithmetic in C++, including how to increment and decrement pointers. Remember, pointer arithmetic can be tricky, so it’s important to understand and practice these concepts.

Next steps for learning could involve diving deeper into pointers in C++, such as understanding the relationship between arrays and pointers, and how to use pointers with functions.

Practice Exercises

  1. Exercise 1: Create an array of integers and a pointer. Move the pointer through the array using increment operation and print each element.
  2. Exercise 2: Create a pointer to the last element of an array. Move the pointer through the array using decrement operation and print each element.

Solutions

  • Solution to Exercise 1:
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; // pointer p points to the first element of the array

for(int i = 0; i < 5; i++)
{
    cout << *p << endl;
    p++; // move the pointer to next element
}

Here, we're moving the pointer p through the array using the increment operation and printing each element.

  • Solution to Exercise 2:
int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[4]; // pointer p points to the last element of the array

for(int i = 4; i >= 0; i--)
{
    cout << *p << endl;
    p--; // move the pointer to previous element
}

In this solution, we're moving the pointer p backwards through the array using the decrement operation and printing each element.

Remember to practice these exercises on your own to reinforce your understanding of pointer arithmetic in C++.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Scientific Calculator

Perform advanced math operations.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help