1 / 14

Step through Array of Structures

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

chi
Télécharger la présentation

Step through Array of Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Step through Array of Structures

  2. Problem: How do we create an array of structures and recall them using pointers?

  3. 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.

  4. 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

  5. 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.

  6. Program Flow Chart Create the Array of structures Define the Structure Establish a Pointer to the Structure Recall the Structure using Pointers

  7. Defining The structure: Creating The Array:

  8. Establishing a Pointer to the Structure: Recalling The Structure:

  9. Program Output:

  10. 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.

  11. Stepping through arrays. Example: Purchasing Proper Rims Before the main function: -Declare structure. -Initialize structure array. -Declare function prototype. -Declare pointer.

  12. 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”

  13. 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.

  14. 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 .

More Related