Question
4.3 Lab: Pointers and Structures new is the operator used to dynamically allocate memory while the program is running delete is the operator used to
4.3 Lab: Pointers and Structures
new is the operator used to dynamically allocate memory while the program is running
delete is the operator used to release memory when it is no longer needed, so it could be used next time new is used
This lab gives you an example of using new and delete with structures.
Your tasks are listed below:
- review the existing code
- find and fix errors
- write additional comments to briefly explain the error and your solution
#include
struct Stu{ string name; double gpa; };
void showStu(const Stu &anyStudent);
int main() {
Stu t = {"Tom", 3.5}; Stu b = {"Bob", 2.9}; Stu *ptrT; Stu *ptrB; ptrT = t; ptrB = b; ptrT = new Stu; ptrB = new Stu; showStu(*ptrT); showStu(*ptrB); return 0; }
/* This function displays all fields of its Stu structure parameter */ void showStu(const Stu &anyStudent) { cout << "Name: " << anyStudent->name << endl; cout << " gpa: " << anyStudent.gpa << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started