0% found this document useful (0 votes)
10 views10 pages

Lab-09 ME-A

The lab manual focuses on teaching students the fundamental concepts of pointers in C++, including declaration, initialization, and usage for memory address manipulation. Students will engage in hands-on exercises to implement pointer-based solutions, learn about pointer arithmetic, dynamic memory allocation, and the relationship between pointers and arrays. The lab aims to enhance students' problem-solving skills and understanding of efficient memory management in C++ programs.

Uploaded by

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

Lab-09 ME-A

The lab manual focuses on teaching students the fundamental concepts of pointers in C++, including declaration, initialization, and usage for memory address manipulation. Students will engage in hands-on exercises to implement pointer-based solutions, learn about pointer arithmetic, dynamic memory allocation, and the relationship between pointers and arrays. The lab aims to enhance students' problem-solving skills and understanding of efficient memory management in C++ programs.

Uploaded by

NAZISH BIBEAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

ME-237 Computer Systems & Programming

LAB MANUAL – 09

Course Instructor: Dr. Sagheer Khan

Lab Instructor: Engr. Eman Fatima

Student Name: _______________________________________________

Degree/ Syndicate: ____________________________________________

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.

 Declaring a pointer → int* ptr; (This means ptr is a pointer to an integer.)


 Assigning an address → ptr = &x; (Now ptr holds the address of x.)
 Dereferencing a pointer → *ptr (This accesses the value stored at the address in ptr.)

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.

2. Assigning an Address to a Pointer

A pointer stores the memory address of a variable. The address-of operator (&) is used
to assign an address to a pointer.

3. Accessing the Value using a Pointer (Dereferencing)

The dereference operator (*) is used to access the value stored at the memory address.

4. Pointer Initialization & Null Pointer

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 (-).

6. Dynamic Memory Allocation (new & delete)

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.

7. Pointers & Arrays

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

Example No. 02: (Assigning an Address to a Pointer)

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.

Example No. 03: (Accessing the Value using a Pointer)

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.

Example No. 04: (Pointer Initialization & Null Pointer)

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.

Example No. 05: (Pointer Arithmetic)

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.

Example No. 07: (Pointers & Arrays)

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.

2. Write a C++ program to demonstrate pointer declaration, assignment, and


dereferencing. Declare an integer variable and a pointer, assign the variable's address to
the pointer, and print both values. Then, declare two integers, use a pointer to swap their
values, and display them before and after swapping.

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

You might also like