140 likes | 280 Views
Step through Array of Structures. Problem: . How do we create an array of structures and recall them using pointers?. Array of structures. Just as there are arrays of integers and character strings, there are also arrays of structures
E N D
Problem: How do we create an array of structures and recall them using pointers?
Array of structures Just as there are arrays of integers and character strings, there are also arrays of structures The value of a member of a structure in an array can be assigned to another variable, or the value of a variable can be assigned to a member.
Pointers to structures A structure can contain a pointer to another structure of its own type or to itself. Using pointers instead of values is much more efficient. Structure data can be input or initialized. Pointers to structures are very important for building linked lists
What does the program do? A structure is defined and then an array of 4 structures is declared and initialized. A pointer to the structure is established The various parts of the structures are then recalled and printed using pointers.
Program Flow Chart Create the Array of structures Define the Structure Establish a Pointer to the Structure Recall the Structure using Pointers
Defining The structure: Creating The Array:
Establishing a Pointer to the Structure: Recalling The Structure:
Stepping through arrays. Example: Purchasing Proper Rims We use a real word situation to show the application of the Arrays of a Structure. Situation : Customer looking to upgrade Rims on his car needs to know what the adequate size for his vehicle is. He approaches a company or a website to obtain the information. A car has many characteristics that identify them. For instance: Year Built, Make, Model, Color, Plate, etc. Because all of these attributes describe a car, we see suitable the use of a structure to store under one data type the full description of a car. Relevant to this particular case we will only use the year built, make and model. See below.
Stepping through arrays. Example: Purchasing Proper Rims Before the main function: -Declare structure. -Initialize structure array. -Declare function prototype. -Declare pointer.
Stepping through arrays. Example: Purchasing Proper Rims Cars in data base: Option 1-BMW Rim 20” Option 3-Dodge Rim 22” Option 2-Cadillac Rim 24”
Stepping through arrays. Example: Purchasing Proper Rims -Declare structure. -Initialize structure array. -Declare pointer. -Declare function prototype. Then the switch statement nested in a do while guarantees us a proper choice.
Stepping through arrays. Lessons Learned. As shown throughout this presentation, we point out once more that while arrays allow for a named collection of identical objects. This is suitable for a limited number of tasks, but isn't really very flexible. Most real data objects are complicated things with an inherent structure that does not fit well on to array style storage. The use of the ‘arrow’ -> operator as opposed to the (*…) was implemented to avoid confusion when using linked lists, which are structures with pointers to other structures. Pointers to structures are the most efficient and most common programming practice nowadays .