Lab-09 ME-A
Lab-09 ME-A
LAB MANUAL – 09
1
LAB # 09: Pointers in C++
Lab Objective:
The main objective of this lab is to familiarize students with the fundamental concepts of
pointers in C++. By the end of this lab, students should be able to effectively declare, initialize,
and use pointers to store and manipulate memory addresses. Additionally, students will learn
how to access and modify variables using pointers, perform pointer arithmetic, and understand
the relationship between pointers and arrays. Through hands-on exercises, students will develop
problem-solving skills by implementing pointer-based solutions for various computational
problems, including dynamic memory allocation and passing pointers to functions.
Hardware/Software Tools:
Hardware: Desktop/Computer
Software Tool: MS Visual Studio 2013
Lab Description:
This lab will provide students with a practical understanding of pointers in C++. Students will
begin by learning how to declare and initialize pointers, assign memory addresses, and use the
dereference operator (*) to access values stored in memory. They will then explore pointer
arithmetic, including incrementing and decrementing pointers to navigate through arrays. The lab
will also cover the use of pointers with arrays, demonstrating how array names act as pointers
and how pointers can be used for efficient array traversal. Additionally, students will work with
dynamic memory allocation using new and delete to manage memory at runtime. By the end of
the lab, students will have a clear understanding of how pointers work and their significance in
efficient memory management within C++ programs.
Pointers:
A pointer is a special type of variable in C++ that does not store an actual value but instead
stores the memory address of another variable. Think of it like a "house address" that tells you
where something is stored rather than what is inside the house. With pointers, you can directly
access and modify data stored in memory, making your programs more efficient and flexible.
For example, if you have a normal variable like int x = 10;, the number 10 is stored in a specific
memory location. Normally, we access x directly, but with a pointer, we can store the address of
x and access it indirectly. This allows us to modify x without referring to it by name.
2
Syntax of Pointers:
1. Declaring a Pointer
data type: Type of values stored in the pointer (e.g., int, float, char).
array name: Name of a pointer.
A pointer stores the memory address of a variable. The address-of operator (&) is used
to assign an address to a pointer.
The dereference operator (*) is used to access the value stored at the memory address.
A pointer should always be initialized. If not assigned, set it to nullptr to avoid accessing
an invalid memory location.
3
5. Pointer Arithmetic
Pointers support arithmetic operations such as increment (++), decrement (--), addition
(+), and subtraction (-).
Pointers can dynamically allocate and deallocate memory. Dynamic memory allocation
allows us to allocate memory at runtime, rather than at compile time. This is useful
when the required memory size is unknown in advance. The new operator is used to
allocate memory dynamically, and the delete operator is used to free the allocated
memory.
Pointers and Arrays are closely related. An array's name acts like a constant pointer to its
first element. This means that using a pointer, we can access and manipulate array
elements.
4
Example No. 01: (Declaring a Pointer)
Code
Output
Code
Output
5
In this example, an integer variable num with a value of 10 and a pointer ptr that stores the
address of num is declared.. The program then displays the memory address of num directly and
through the pointer ptr, showing how pointers store and reference memory addresses.
Code
Output
In this example, an integer variable num with a value of 20 and a pointer ptr that stores the
memory address of num is declared. The program then displyas the value of num using the
pointer by dereferencing it with *ptr, showing how pointers can be used to access variable values
indirectly.
Code
6
Output
This example firstly declares a null pointer ptr1, which means it does not point to any valid
memory address. Then, it declares an integer variable num with a value of 30 and assigns its
address to the pointer ptr2. The program displays the value of ptr1 (which is nullptr) and the
address stored in ptr2, along with the value it points to (which is num). This is how pointers can
store addresses and be used to access variable values.
Code
Output
In this example, it firstly initializes an integer array arr with four values and declares a pointer
ptr that initially points to the first element. The program displays the first element using *ptr,
then increments ptr to move to the next element and displays it.
7
Example No. 06: (Dynamic Memory Allocation [new & delete])
Code
Output
This code creates an integer pointer ptr and allocates memory for an integer. The value 50 is
then assigned to the allocated memory and displayed. Finally, the delete keyword is used to free
the dynamically allocated memory, preventing memory leaks.
Code
8
Output
This code initializes an integer array arr with four values and declares a pointer ptr that points
to the first element. A for loop iterates through the array, using *(ptr + i) to access each element
by shifting the pointer position. The program displays all array elements sequentially, showing
how pointers can traverse an array efficiently.
9
Lab Tasks:
1. Write a C++ program that demonstrates pointer declaration and dereferencing. Declare
an integer variable, assign a value to it, and then create a pointer that points to the integer.
Use the pointer to print the value of the integer using dereferencing.
3. Write a C++ program that demonstrates the use of pointers to manipulate the contents of
an array. You are required to:
Declare an array of integers with a size of your choice.
Utilize a pointer to access each element of the array.
Modify the values of the array elements through pointer dereferencing.
Display the modified contents of the array.
10