allocate - my_ptr->element_name =...

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-&gt;element_name = &quot;Nitrogen&quot;; my_ptr-&gt;atomic_weight = 14.0067; // Display members of the dynamically-allocated structure. cout &lt;&lt; &quot;Element Name: &quot; &lt;&lt; my_ptr-&gt;element_name &lt;&lt; endl; cout &lt;&lt; &quot;Atomic Weight: &quot; &lt;&lt; my_ptr-&gt;atomic_weight &lt;&lt; endl; delete my_ptr; // Free memory used by the data. } else { cout &lt;&lt; &quot;Memory allocation was unsuccessful.\n&quot;; } return 0; }...
View Full Document

Ask a homework question - tutors are online