0% found this document useful (0 votes)
0 views16 pages

Chapter-6

This document provides an overview of pointers in C++, including their definition, usage, and the relationship between pointers and memory addressing. It covers key concepts such as pointer variables, the address-of operator, and dereferencing, along with practical examples and exercises. The chapter aims to equip students with the skills to effectively use pointers in programming.

Uploaded by

mamoyoseph16
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views16 pages

Chapter-6

This document provides an overview of pointers in C++, including their definition, usage, and the relationship between pointers and memory addressing. It covers key concepts such as pointer variables, the address-of operator, and dereferencing, along with practical examples and exercises. The chapter aims to equip students with the skills to effectively use pointers in programming.

Uploaded by

mamoyoseph16
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SECT1082

Computer Programming
Chapter-V: Pointers in C++

Mr. Khalid. A
School of Electrical, Computer & Biomedical Engineering
Haramaya Institute of Technology
Haramaya University
4/4/2026 Mr. Khalid A. 1
Outline
•Introduction to Pointers
•Definition of Pointers
•Pointer Usage
•Pointer Variables
•Memory Addressing
•Address Operator (&)

4/4/2026 Mr. Khalid A. 2


Class Objectives
After completing this chapter, students should be able to:
•Understand the concept of memory addressing
•Define and use pointers
•Declare pointer variables correctly
•Use the address-of operator (&) effectively
•Understand the relationship between variables and memory locations

4/4/2026 Mr. Khalid A. 3


1. Introduction to Pointers
➢ In C++, every variable is stored in memory. ➢ A pointer is a variable that stores the
➢ Each memory location has a unique memory address of another variable.
address. Example int x = 10;
➢ Normally, we use variables to store values: int *p = &x;
int x = 10; Explanation: x → normal variable
➢ But sometimes, we need to work with the &x → address of x
memory address itself instead of the value. p → pointer storing the address of x
➢ This is where pointers are used. Important Symbols
Key Idea Symbol Meaning
•A variable → stores a value * pointer declaration / dereferencing
4/4/2026 Mr. Khalid A. & address-of operator 4
•A pointer → stores an address
2. Pointer Variables
Memory Concept Declaration of Pointer
➢ Before understanding pointers, it is Syntax data_type *pointer_name;
important to understand memory Examples int *p;
float *fptr;
addressing. char *cptr;
Example int x = 10; Initialization of Pointer
•x is stored somewhere in memory A pointer must be assigned a valid address
•That memory location has an address (for before use. int x = 10;
example: 1000) int *p = &x;
So we can think: Explanation
Variable Value Address •p stores the address of x
x 10 1000 •*p gives the value stored at that address
4/4/2026 Mr. Khalid A. 5
2. Pointer Variables…
Accessing Value Using Pointer(Dereferencing) Output Explanation
Dereferencing means accessing the value •x → prints value
stored at the address. •&x → prints address
#include <iostream> •p → same address
using namespace std; •*p → prints value again
int main() Memory Addressing
{ Every variable in C++ has a memory address.
int x = 10; Example int a = 5;
int *p = &x; We can access its address using: cout << &a;
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl; Key Points
cout << "Pointer p: " << p << endl; •Memory is organized into small units called
cout << "Value using pointer: " << *p bytes
<< endl; •Each byte has a unique address
return 0; •Pointers help us work directly with these
}
4/4/2026
addresses
Mr. Khalid A. 6
3. Address of Operator(&)
➢ The address-of operator (&) is used to get Example Summary int x = 10;
the memory address of a variable. int *p = &x;
Syntax &variable_name Expression Meaning
Example int x = 10; x value (10)
cout << &x; &x address of x
Explanation p stores address
•&x returns the memory location where x is stored *p value at address
Relationship Between Variable and Pointer
This chapter forms the foundation for
Concept Description advanced topics such as:
Variable Stores a value •pointer arithmetic
Address Location of variable in memory •arrays and pointers
Pointer Stores address of variable •dynamic memory allocation
Dereference (*) Access value using pointer •linked data structures
4/4/2026 Mr. Khalid A. 7
3. Address of Operator(&)…
Common Mistakes ➢ are used in dynamic memory allocation
Using Uninitialized Pointer ➢ improve performance in some cases
int *p; ➢ are essential for advanced topics like:
*p = 10; // ❌ ERROR
• arrays
Correct Way
int x = 10; • functions
int *p = &x; • data structures
Confusing * and & Summary
•* → value at address In this chapter, we studied pointers in C++.
•& → address of variable •A pointer is a variable that stores an address
Advantages of Pointers •& gives the address of a variable
Pointers are important because they: •* is used to declare and access pointer values

➢ Allow direct memory access


4/4/2026
•Pointers connect variables with memory locations
Mr. Khalid A. 8
Exercises
Write a program to change the value of a
variable using a pointer.
#include <iostream>
using namespace std;

int main()
{
int x = 5; Output
int *p = &x; Updated value of x = 20

*p = 20; Explanation
•*p = 20 changes the value stored
cout << "Updated value of x = " << x;
at address of x
return 0; •So x becomes 20
}
4/4/2026 Mr. Khalid A. 9
Exercises…
Pointer with Array (Intermediate) Output 10
Write a program to display elements of an 20
array using a pointer. 30
#include <iostream> 40
using namespace std; 50
int main() p
{ ↓
int arr[5] = {10, 20, 30, 40, 50}; [10] [20] [30] [40] [50]
int *p = arr; ↑ ↑ ↑ ↑ ↑
// int size of 4 bytes & memory allocate p p+1 p+2 p+3 p+4
the first index of arr
Explanation
for (int i = 0; i < 5; i++)
{ •p = arr → pointer points to first element
cout << *(p + i) << endl; •*(p + i) → accesses elements using
} pointer arithmetic
4/4/2026 return 0; Mr. Khalid A.
•Works exactly like arr[i] 10
Exercises…
Swapping Two Variables Using Pointers swap(&x, &y);
Write a program to swap two numbers using
pointers. cout << "x = " << x << endl;
#include <iostream> cout << "y = " << y << endl;
using namespace std;
return 0;
void swap(int *a, int *b) }
{
Output x = 20
int temp = *a;
*a = *b; y = 10
*b = temp; Explanation
}
•&x, &y → addresses passed to function
int main() •*a, *b → access actual values
{
•Values are swapped using pointers
4/4/2026 int x = 10, y = 20; Mr. Khalid A. 11
Exercises…
Before: Pointer to Pointer (Advanced)
x = 10 y = 20 Write a program to demonstrate a pointer to a
pointer.
a→x b→y #include <iostream>
using namespace std;
Inside function: int main()
*a = 10 *b = 20 {
int x = 10;
After swap: int *p = &x;
*a = 20 *b = 10 int **pp = &p;

Result: cout << "Value of x = " << x << endl;


x = 20 y = 10 cout << "Using p = " << *p << endl;
cout << "Using pp = " << **pp << endl;
return 0;
4/4/2026
}
Mr. Khalid A. 12
Exercises…
Diagram (Step-by-step)
Output
pp --------> p --------> x
Value of x = 10 (3000) (2000) (1000)
Using p = 10 |

Using pp = 10 10
Value Access
Memory Table x = 10
Address Variable Value *p = 10
**pp = 10
------- -------- ----- Explanation
1000 x 10 •p → pointer to x
2000 p 1000 •pp → pointer to pointer p

3000 pp 2000 •*p → value of x


4/4/2026 Mr. Khalid A.
•**pp → value of x again 13
Summary of Difficulty Progression
Key Takeaways
Level Concept
•& → address of variable
1 Basic pointer usage
•* → value at address
Modifying value using
2
•Pointers can: pointer
•modify variables 3 Pointer with arrays
Pointers in functions
•work with arrays 4
(swap)
•be passed to functions 5 Pointer to pointer
•point to other pointers

4/4/2026 Mr. Khalid A. 14


Summary of Difficulty Progression…

4/4/2026 Mr. Khalid A. 15


4/4/2026 Mr. Khalid A. 16

You might also like