Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include using namespace std;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions