Learn with Yasir

Share Your Feedback

Lecture 7: Introduction to Pointers in C++


Lecture 7: Introduction to Pointers in C++

Course: CS-303 Programming Fundamentals
Topics: Memory Addresses, Pointer Variables, Address-of Operator and Dereferencing

1. Learning Outcomes

After this lecture, students will be able to:

  1. Understand how variables are stored in computer memory.
  2. Explain memory addresses.
  3. Define pointers in C++.
  4. Declare and initialize pointer variables.
  5. Use the address-of operator (&).
  6. Use the dereference operator (*).
  7. Access and modify variable values through pointers.

2. Introduction: How Does Computer Memory Work?

Whenever we declare a variable in C++, the computer reserves memory to store its value.

For example:

int age = 20;

The computer stores the value 20 in a memory location.

Each memory location has an address.

Think of computer memory like a house:

  • Variable = House
  • Value = Person living in the house
  • Memory address = House address

Example: Variable in Memory

1000

Memory address

20

Stored value

Illustrative memory address only

Variable: age

The actual memory address is assigned by the computer. It may be different every time the program runs.

3. What is a Memory Address?

A memory address is a location in computer memory where a variable is stored.

We can display the address of a variable using the address-of operator &.

Example 1: Displaying a Variable Address

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    cout << "Age: " << age << endl;

    cout << "Address: " << &age << endl;

    return 0;
}

Possible Output:

Age: 20
Address: 0x7ffe1234

The address shown above is only an example. Your computer will display a different address.

Understanding the Code

Expression Meaning
age Value of the variable
&age Memory address of the variable
20 Value stored in the variable

Remember: The & operator is called the address-of operator when used with a variable.

4. What is a Pointer?

A pointer is a variable that stores the memory address of another variable.

In simple words:

  • A normal variable stores a value.
  • A pointer variable stores an address.

Normal Variable

age

20

Stores a value

Pointer Variable

ptr

Address of age

Stores an address

5. Pointer Declaration

The general syntax of a pointer is:

data_type *pointer_name;

Examples:

int *ptr;
float *fptr;
char *cptr;
double *dptr;

Explanation:

  • int *ptr declares a pointer to an integer.
  • float *fptr declares a pointer to a float.
  • char *cptr declares a pointer to a character.
  • double *dptr declares a pointer to a double.

A pointer should point to a compatible data type.

6. Initializing a Pointer

We can store the address of a variable in a pointer using the address-of operator &.

Example 2: Basic Pointer Program

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    int *ptr = &age;

    cout << "Age: " << age << endl;

    cout << "Address of age: " << &age << endl;

    cout << "Pointer value: " << ptr << endl;

    return 0;
}

Possible Output:

Age: 20
Address of age: 0x7ffe1234
Pointer value: 0x7ffe1234

Explanation

int age = 20;

Creates an integer variable.

int *ptr = &age;

Creates a pointer and stores the address of age.

Notice that:

ptr == &age

Both expressions represent the same address.

7. Dereferencing a Pointer

Dereferencing means accessing the value stored at the memory address held by a pointer.

We use the * operator to dereference a pointer.

Example 3: Accessing a Value Through a Pointer

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    int *ptr = &age;

    cout << "Value of age: " << age << endl;

    cout << "Value using pointer: " << *ptr << endl;

    return 0;
}

Output:

Value of age: 20
Value using pointer: 20

Understanding Three Important Expressions

Expression Meaning
age Value of age
&age Address of age
*ptr Value stored at the address held by ptr

Important: The symbol * has two uses in these examples:

  1. In int *ptr, it declares a pointer.
  2. In *ptr, it dereferences a pointer.

8. Modifying a Variable Through a Pointer

A pointer can also modify the original variable.

Example 4

#include <iostream>
using namespace std;

int main()
{
    int number = 10;

    int *ptr = &number;

    cout << "Before modification: " << number << endl;

    *ptr = 50;

    cout << "After modification: " << number << endl;

    return 0;
}

Output:

Before modification: 10
After modification: 50

Explanation

Initially:

number = 10;

The pointer stores the address of number.

*ptr = 50;

This changes the value stored at that address.

Therefore, the original variable number becomes 50.

9. Difference Between Normal Variables and Pointers

Normal Variable Pointer Variable
Stores a value Stores a memory address
Example: int x = 10; Example: int *ptr = &x;
Access value using variable name Access pointed value using *ptr
Address obtained using &x Address stored in ptr

10. Dry Run: Understanding Pointers

Consider this program:

int number = 25;

int *ptr = &number;

*ptr = 40;

Dry run:

Step Statement number ptr
1 int number = 25; 25 Not declared
2 int *ptr = &number; 25 Address of number
3 *ptr = 40; 40 Address of number

Final value:

number = 40;

11. Common Pointer Mistakes

Mistake 1: Using an Uninitialized Pointer

Incorrect:

int *ptr;

*ptr = 20;

The pointer does not have a valid address. Dereferencing it causes undefined behavior.

Correct:

int number = 10;

int *ptr = &number;

*ptr = 20;

Mistake 2: Using an Incompatible Pointer Type

Incorrect:

float number = 10.5;

int *ptr = &number;

Correct:

float number = 10.5;

float *ptr = &number;

Mistake 3: Dereferencing a Null Pointer

Incorrect:

int *ptr = nullptr;

cout << *ptr;

A null pointer does not point to a valid object. Dereferencing it causes undefined behavior.

Correct:

int *ptr = nullptr;

if (ptr != nullptr)
{
    cout << *ptr;
}

12. Programming Exercises

Problem Statement 1: Display Variable Address

Write a C++ program to display the value and memory address of an integer variable.

Tasks:

  1. Declare an integer variable named number.
  2. Assign it a value of 100.
  3. Display its value.
  4. Display its memory address using &.

Requirements:

  • Use the address-of operator.
  • Use appropriate output statements.

Problem Statement 2: Access Value Using Pointer

Write a C++ program to access an integer variable through a pointer.

Tasks:

  1. Declare an integer variable named number.
  2. Assign it a value of 50.
  3. Declare an integer pointer.
  4. Store the address of number in the pointer.
  5. Display the value using the pointer.

Requirements:

  • Use a pointer variable.
  • Use the dereference operator.

Problem Statement 3: Modify Value Using Pointer

Write a C++ program to modify a variable using a pointer.

Tasks:

  1. Declare an integer variable with value 10.
  2. Create a pointer to the variable.
  3. Change its value to 100 using the pointer.
  4. Display the original variable.

Requirements:

  • Do not directly assign 100 to the original variable.
  • Modify it through the pointer.

Problem Statement 4: Swap Two Numbers Using Pointers

Write a C++ program to swap two numbers using pointers.

Tasks:

  1. Input two integer values.
  2. Create pointers to both variables.
  3. Swap their values using pointers.
  4. Display the values before and after swapping.

Requirements:

  • Use pointer variables.
  • Use the dereference operator.
  • Do not use a separate swap function in this exercise.

13. Classroom Questions

  1. What is a memory address?
  2. What is a pointer?
  3. What is the purpose of the & operator?
  4. What is the purpose of the * operator?
  5. What is the difference between ptr and *ptr?
  6. Can a pointer store the address of a character variable?
  7. What happens when we dereference an uninitialized pointer?
  8. Can a pointer modify the original variable?
  9. What is nullptr?
  10. What is the difference between a normal variable and a pointer?

14. Quick Quiz

Test your understanding

0 / 6 answered

1. What does a normal integer variable store?

A. Memory address

B. Integer value

C. Function name

D. Pointer type

2. What does a pointer store?

A. Only decimal values

B. A memory address

C. A function output

D. A string

3. Which operator returns the address of a variable?

A. *

B. &

C. %

D. !

4. What does *ptr do?

A. Gets the value at the address stored in ptr

B. Gets the address of ptr

C. Deletes the variable

D. Declares a function

5. What happens when we execute *ptr = 50?

A. The pointer address becomes 50

B. The pointed variable gets the value 50

C. The pointer is deleted

D. The program always stops

6. Which value represents a null pointer?

A. 0.5

B. nullptr

C. true

D. 50

Submit Answers

15. Lecture Summary

In this lecture, we learned:

Concept Meaning
Memory Address Location of a variable in memory
Pointer Variable that stores an address
& Address-of operator
* Dereference operator
nullptr Represents a null pointer

Key Message:

A normal variable stores a value, while a pointer stores the memory address of another variable. Pointers allow us to access and modify data through memory addresses.