1 / 21

Stepping through an Array of Structures

Stepping through an Array of Structures. Group IV Daniel Marquez Raheel Kapadia Victor Polanco. General Overview. OVERVIEW (Lines 1-16) #include <stdio.h> #define MAX 4 struct part { int number; char name[10]; }; struct part data[MAX] = {

cassie
Download Presentation

Stepping through an 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. Stepping through an Array of Structures Group IV Daniel Marquez Raheel Kapadia Victor Polanco

  2. General Overview OVERVIEW (Lines 1-16) • #include <stdio.h> • #define MAX 4 • struct part { • int number; • char name[10]; • }; • struct part data[MAX] = • { • {1, "Smith"}, • {2, "Jones"}, • {3, "Adams"}, • {4, "Wilson"} • }; OVERVIEW CONT. (lines 17-32) • struct part *p_part; • int count; • int main(void) • { • p_part = data; • for (count = 0; count < MAX; count++) • { • printf("\nAt address %p: %d %s", p_part, p_part->number, • p_part->name); • p_part++; • } • }

  3. BlockDiagram

  4. Code in DetailLine 1:#include <stdio.h> Includes the standard I/O library into the program Is a part of the IDE Stands for: Standard Input/Output Header Only library necessary for program

  5. Line 3: #define MAX 4 In this case, MAX is the macro-name and 4 is the character sequence No semicolon is needed for a #define statement Every time the words MAX are encountered in the source file, they will be replaced with the number 4 This is a global definition, so it will be usable throughout the entire program

  6. Line 5: struct part { • This declares a global structure in the program called ‘part’, called a tag • A structure is a collection of variables referenced under one name • The squiggly braces represent the structure having more than one element

  7. Line 6:int number; • This is the first member of structure ‘part’ • It is an integer called ’number’ • This variable is not yet created • Declaring a structure means you are defining an aggregate type

  8. Line 7:char name[10]; This is the second member of structure ‘part’ It is an character array called ’name’ This array has 10 elements This character array has not yet been created

  9. Line 8:}; This will complete the structure, ‘part’ The structure was composed of two elements A semicolon is needed to terminate the structure since the structure declaration was a statement

  10. Line 10:struct part data[MAX] = { • Declares an array of MAX = 4 structures • Part structure defined as having 2 fields, int number and char name[10] • Data is defined as {1, "Smith"}, {2, "Jones"}, {3, "Adams"}, {4, "Wilson“} • Closing bracket and semicolon declare the end of scope and definition of data

  11. Line 18:struct part *p_part; int count; • Declares a pointer to type part named *p_part • Declares an integer named count • Fields are to be used later in the program during the step through and printing of the array of structures

  12. Main Function() & Components

  13. BRIEFLY COVER THE MAIN FUNCTION • THIS FUNCTION TELLS THE PROGRAM WHERE TO START • IS KEPT SHORT AND CALLS ON OTHER FUNCTION • ALL C CODES MUST HAVE A MAIN FUNCTION

  14. Initializing the Pointer Variable data of type part was declared A structure with type part was declared p_part is assigned the first element of data A pointer to type part is declared

  15. For Loop • The for loop will continue through its sequence as long as count is less than MAX • MAX is set to 4 • The printf function is executed four times

  16. Printf takes a string of characters between quotation marks, and outputs them • to the screen. • "\n" This sequence simply means move to the next line. • The %p, %d, %s refer to pointer, integer, string. • p_part refers to address • The arrow operator is used with a pointer to a structure. • Name and Number pertain to the members of structure part

  17. Output of the Program

  18. Similar Code #include <stdio.h> struct stores { int number; char address[20]; char store_name[15]; }; struct stores whitepages[6] = { {1, "1256 NW 21 St", "CompUSA"}, {20, "1234 SW Eagle Drive", "Tiger Direct"}, {19, "1234 SW Thomas Road", "Publix"}, {9, "1234 SW 24 Blvd", "CVS"}, {6, "1234 SW 13 Ave", "Old Navy"}, {10, "1234 SW 107 Terr", "Radio Shack"} }; struct stores *yellowpages; int count; int main(void) { yellowpages = whitepages; for (count = 0; count < 6; count++) { printf("\nOn Page %d: %s\n%s\n", yellowpages->number, yellowpages->store_name, yellowpages->address); printf("Address of yellowpages: %p\n", yellowpages); yellowpages++; printf("Address of whitepages: %p\n", &whitepages[count]); } }

  19. Output

  20. LESSONS LEARNED • FLEXIBILITY OF STRUCTURES • THE GROUPING OF VARIABLES UNDER ONE NAME • POINTERS ALLOW A FUNCTION TO MODIFY A VARIABLE PASSED TO IT. • WHEN DECLARING A STRCTURE, THE ELEMENTS WITHIN IT ARE’NT ACTUALLY CREATED, BUT RATHER, ONLY THE FORM OF THE DATA HAS BEEN DEFINED.

  21. REFERENCES • http://www.loirak.com/prog/ctutor.php#first • Deitel, DietelC How To Program Fifth Edition. Pearson Prentice Hall, 2007.

More Related