Lab 1 - Pointer Lab Intro
Objective
In essence, a pointer is a variable that stores the memory address of another variable. Using pointers allows you to directly manipulate the memory locations of variables, allowing you to manage memory and build complex data structures efficiently.
In this lab, you will practice the fundamentals of pointers, including how to declare and initialize pointers, access the memory address of a variable, and use pointers to manipulate data.
Understand how to declare and initialize pointers.
Use pointers to access and modify variables.
Understand the difference between stack and heap memory.
Perform dynamic memory allocation and deallocation using
new
anddelete
.
Requirements
Basic Pointer Operations
Section 1
Declare an integer variable named
number
and initialize it to 10.Declare a pointer variable named
ptr
that points tonumber
.Print the value of
number
using both the variable itself and the pointer.Modify the value of
number
to 20 using the pointer.Print the modified value of
number
using both the variable and the pointer.
Section 2
Declare a pointer variable named
dynPtr
and dynamically allocate memory for an integer using thenew
operator.Assign the value 30 to the allocated memory.
Print the value stored in the dynamically allocated memory.
Deallocate the memory using the
delete
operator and setdynPtr
tonullptr
.Print a message confirming the memory deallocation.
Section 3
Declare a pointer variable named
arrayPtr
and dynamically allocate memory for an array of 5 integers using thenew
operator.Assign values to the array elements (e.g., 1, 2, 3, 4, 5).
Print the values of the array elements using the pointer.
Deallocate the memory using the
delete[]
operator and setarrayPtr
tonullptr
.Print a message confirming the memory deallocation.
Use the following code starter to complete your lab. Please make sure you add the C++ code under the pseudo-coded instructions:
// Author: <Your Name>
// Date: <date>
// Lab - Pointers Lab 1
// Purpose: Introduction demonstration of using pointers
#include <iostream>
using namespace std;
int main() {
/*
Section 1 Activities
*/
cout << "Section 1" << endl;
// Step 1: Declare and initialize integer variable
// Step 2: Declare pointer and initialize it with the address of number
// Step 3: Print the value of number using the variable and the pointer
// Step 4: Modify the value of number to 20 using the pointer
// Step 5: Print the modified value of number using the variable and the pointer
cout << endl;
/*
Section 2 Activities
*/
cout << "Section 2" << endl;
// Step 1: Dynamically allocate memory for an integer
// Step 2: Assign the value 30 to the allocated memory
// Step 3: Print the value stored in the dynamically allocated memory
// Step 4: Deallocate the memory and set the pointer to nullptr
// Step 5: Print a message confirming the memory deallocation
cout << endl;
/*
Section 3 Activities
*/
cout << "Section 3" << endl;
// Step 1: Dynamically allocate memory for an array of 5 integers
// Step 2: Assign values to the array elements
// Step 3: Print the values of the array elements using the pointer
cout << "Array values: ";
cout << endl;
// Step 4: Deallocate the memory and set the pointer to nullptr
// Step 5: Print a message confirming the memory deallocation
return 0;
}
Output of Code:
Deliverable
Upload the following:
Full source code (.cpp files or .txt files)
Screenshot of the Console with the code executing
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark