LB 12
LB 12
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.
OUTPUT:
PROGRAM:
OUTPUT:
OUTPUT:
EXAMPLE 4: Pointer initialization.
PROGRAM:
OUTPUT:
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: