allocate - my_ptr>element_name ="Nitrogen"...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// allocate.cpp / #include<iostream.h> #include"apstring.h" # struct chemical_element { apstring element_name; double atomic_weight; }; int main() { chemical_element *my_ptr; // Declare pointer to point to the // dynamically-allocated structure. my_ptr = new chemical_element; // Allocate memory for structure. if(my_ptr != NULL) // Check to make sure allocation was successful. { // Initialize members of the structure.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: my_ptr->element_name = "Nitrogen"; my_ptr->atomic_weight = 14.0067; // Display members of the dynamically-allocated structure. cout << "Element Name: " << my_ptr->element_name << endl; cout << "Atomic Weight: " << my_ptr->atomic_weight << endl; delete my_ptr; // Free memory used by the data. } else { cout << "Memory allocation was unsuccessful.\n"; } return 0; }...
View Full Document

Ask a homework question - tutors are online