0% found this document useful (0 votes)
11 views7 pages

LB 12

This document outlines Lab #12 focused on the implementation of pointers in C++. It covers key concepts such as memory management, pointer declaration and initialization, dereferencing, pointer arithmetic, and their relationship with arrays. The lab includes examples and tasks for practical application, reinforcing the understanding of pointers in C++ programming.

Uploaded by

hamzachlhr111
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)
11 views7 pages

LB 12

This document outlines Lab #12 focused on the implementation of pointers in C++. It covers key concepts such as memory management, pointer declaration and initialization, dereferencing, pointer arithmetic, and their relationship with arrays. The lab includes examples and tasks for practical application, reinforcing the understanding of pointers in C++ programming.

Uploaded by

hamzachlhr111
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
You are on page 1/ 7

Lab # 12

Implementation of Pointers in C++


Objectives:
• Understand how variables are stored in memory and how to access their
addresses.
• Learn how to declare, initialize, and use pointers in C++.
• Use the reference operator (&) to get the address of a variable.
• Use the dereference operator (*) to access or modify the value at a pointer's
address.
• Perform input/output operations using pointers.
• Understand the relationship between pointers and arrays.
• Use pointer arithmetic to traverse arrays.
• Pass variables to functions using pointers and modify their values.
• Learn to handle null pointers safely.
• Build a strong foundation for advanced topics like dynamic memory allocation and
data structures.

Introduction:
In C++, memory management and the use of pointers are fundamental concepts that help
programmers interact directly with the computer’s memory. Every variable in a program is
stored at a unique memory address, which can be accessed using the reference operator
(&). A pointer is a special variable that stores the memory address of another variable, and
it is declared using an asterisk (*). Once a pointer is initialized with the address of a
variable, the dereference operator (*) can be used to access or modify the value stored at
that address. This mechanism allows indirect access to variables and supports powerful
operations like dynamic memory allocation, function argument manipulation, and
efficient array processing. Pointers and arrays are closely related—an array name in C++
represents the base address of the array, so it can be assigned directly to a pointer without
using the reference operator. Additionally, pointer arithmetic enables traversal of arrays by
incrementing or decrementing pointer values. A null pointer, often initialized as NULL,
indicates that a pointer currently points to no valid memory location. Understanding these
terminologies—such as memory address, reference, pointer, dereferencing, and
pointer initialization—is essential for writing efficient, flexible, and memory-aware C++
programs.

EXAMPLE 1: Accessing variable address in memory .


PROGRAM:

OUTPUT:

EXAMPLE 2: Write a program that inputs a number in an integer variable.


It stores the address of the variable in a pointer and then displays the value
and address of the variable.

PROGRAM:
OUTPUT:

EXAMPLE 3:Use pointer to find sum of two integers.


PROGRAM:

OUTPUT:
EXAMPLE 4: Pointer initialization.
PROGRAM:

OUTPUT:

EXAMPLE 5: how to traverse an array using pointers.


PROGRAM:

OUTPUT:
LAB TASK
1. Write a program that inputs five floating points values in an array and displays the values in the
reverse order.

PROGRAM:

OUTPUT:

2. Write a program that inputs a string value from the user and displays it using pointers.

PROGRAM:
OUTPUT:

LAB SESSION
1. Write a program that declares and initializes a string. It inputs a character from the
user and searches the character in the array.
PROGRAM:
OUTPUT:

2. Write a program that inputs two integers and passes them to a function using
pointers. The function exchanges the values, and the program finally displays the
values.
PROGRAM:

OUTPUT:

You might also like