Performing Pointer Arithmetic in C++

Tutorial 2 of 5

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++.