Understanding Pointers and Address Manipulation

Tutorial 1 of 5

Understanding Pointers and Address Manipulation

1. Introduction

Goal

This tutorial aims to explain the concept of pointers and how they can be used to manipulate addresses in memory using C++.

Learning Outcomes

By the end of this tutorial, you will understand what pointers are, how to declare and use them, and how they can be used to manipulate addresses in memory.

Prerequisites

Basic knowledge of C++ programming language is required. Familiarity with data types, variables, and functions would be beneficial.

2. Step-by-Step Guide

Understanding Pointers

A pointer is a variable that stores the memory address of another variable. The type of the pointer has to match the type of the variable whose address it stores.

To declare a pointer, we use the asterisk * before the variable name. For example, to declare a pointer to an integer, we would write int *p;.

Address of Operator (&)

The address of operator & is used to get the memory address of a variable. For example, &num would give the memory address where the variable num is stored.

Dereferencing Operator (*)

The dereferencing operator * is used to get the value stored at the memory address that the pointer points to. For example, if p is a pointer to num, then *p would give the value of num.

Address Manipulation

Pointers can be used to manipulate memory addresses. By using pointer arithmetic (incrementing or decrementing a pointer), we can access and manipulate data stored in contiguous memory locations, such as arrays.

3. Code Examples

Example 1: Declaring and Using Pointers

int num = 10;  // Declare an integer
int *p = # // Declare a pointer to the integer

cout << "Address of num: " << &num << endl; // Print the address of num
cout << "Value of p: " << p << endl; // Print the value of p (which is the address of num)

cout << "Value of num: " << num << endl; // Print the value of num
cout << "Value at address stored in p: " << *p << endl; // Print the value at address stored in p (which is the value of num)

Expected Output

The exact addresses will vary, but the output will be similar to:

Address of num: 0x7fff5fbff710
Value of p: 0x7fff5fbff710
Value of num: 10
Value at address stored in p: 10

Example 2: Address Manipulation

int arr[3] = {1, 2, 3}; // Declare an array
int *p = arr; // Declare a pointer to the array

for (int i = 0; i < 3; i++) {
    cout << *(p + i) << endl; // Print the value at address (arr + i)
}

Expected Output

1
2
3

4. Summary

In this tutorial, we learned about pointers and how to use them to manipulate memory addresses. We discussed how to declare pointers, use the address of and dereferencing operators, and perform pointer arithmetic to manipulate addresses.

The next steps would be to learn about more advanced topics, such as dynamic memory allocation using pointers, and to practice your skills with more complex examples and exercises.

5. Practice Exercises

Exercise 1

Declare a pointer to a float and use it to change the value of the float.

Exercise 2

Declare an array of integers and use a pointer to print the elements of the array.

Exercise 3

Create a function that takes a pointer to an integer as a parameter and changes the value of the integer.

Solution to Exercise 1

float num = 5.0;
float *p = &num;
*p = 6.0;  // Change the value of num
cout << num << endl;  // Should print 6.0

Solution to Exercise 2

int arr[3] = {1, 2, 3};
int *p = arr;
for (int i = 0; i < 3; i++) {
    cout << *(p + i) << endl;
}

Solution to Exercise 3

void changeValue(int *p) {
    *p = 10;
}

int main() {
    int num = 5;
    changeValue(&num);
    cout << num << endl;  // Should print 10
}