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.

  1. Understand how to declare and initialize pointers.

  2. Use pointers to access and modify variables.

  3. Understand the difference between stack and heap memory.

  4. Perform dynamic memory allocation and deallocation using new and delete.

 Requirements

Basic Pointer Operations

Section 1

  1. Declare an integer variable named number and initialize it to 10.

  2. Declare a pointer variable named ptr that points to number.

  3. Print the value of number using both the variable itself and the pointer.

  4. Modify the value of number to 20 using the pointer.

  5. Print the modified value of number using both the variable and the pointer.

Section 2

  1. Declare a pointer variable named dynPtr and dynamically allocate memory for an integer using the new operator.

  2. Assign the value 30 to the allocated memory.

  3. Print the value stored in the dynamically allocated memory.

  4. Deallocate the memory using the delete operator and set dynPtr to nullptr.

  5. Print a message confirming the memory deallocation.

Section 3

  1. Declare a pointer variable named arrayPtr and dynamically allocate memory for an array of 5 integers using the new operator.

  2. Assign values to the array elements (e.g., 1, 2, 3, 4, 5).

  3. Print the values of the array elements using the pointer.

  4. Deallocate the memory using the delete[] operator and set arrayPtr to nullptr.

  5. 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:

image-20240514-134342.png

 

Deliverable

Upload the following:

  1. Full source code (.cpp files or .txt files)

  2. Screenshot of the Console with the code executing

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark