Lab Practice Manual – Pointers in C++
Course: C++ Programming Fundamentals
Topic: Pointers
Duration: 2 Hours
Level: Beginner
Lab Objectives
• Understand pointer declaration and initialization
• Use pointers with variables and arrays
• Print addresses and values using pointers with cout/cin and printf/scanf
• Access arrays using pointers
• Work with character pointers as strings (input/output)
Lab Schedule Overview
Time Slot Activity
0:00–0:20 Pointer Declaration & Initialization
0:20–0:40 Pointer and Variable Interaction
(cout/printf)
0:40–1:10 Pointers with Arrays – Traversal and
Access
1:10–1:30 Character Pointers and Strings
1:30–2:00 Assignment and Q/A
Practice 1: Pointer Declaration and Initialization
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* ptr = &x;
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
cout << "Pointer (ptr): " << ptr << endl;
cout << "Value at pointer: " << *ptr << endl;
return 0;
}
Practice 2: Using Pointers with scanf and printf
#include <stdio.h>
int main() {
int a;
int *p = &a;
printf("Enter an integer: ");
scanf("%d", p);
printf("You entered: %d\n", *p);
printf("Address: %p\n", p);
return 0;
}
Practice 3: Printing Array with Pointers
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
cout << "Array using pointers:\n";
for (int i = 0; i < 5; i++) {
cout << *(ptr + i) << " ";
}
cout << endl;
return 0;
}
Practice 4: Using Pointers with char Strings
#include <iostream>
using namespace std;
int main() {
char name[20];
char *ptr = name;
cout << "Enter your name: ";
cin >> ptr;
cout << "Hello, " << ptr << "!" << endl;
return 0;
}
Assignment
Complete the following tasks using pointers in C++:
• Create a pointer to a float and use it to input and display a value.
• Write a program to reverse an array using pointers.
• Use a pointer to a character array to count the number of vowels in a string.
• Create a function that accepts a pointer to an array and prints even numbers only.