210 likes | 323 Views
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] = {
E N D
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] = • { • {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++; • } • }
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
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
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
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
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
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
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
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
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
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
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
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
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]); } }
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.
REFERENCES • http://www.loirak.com/prog/ctutor.php#first • Deitel, DietelC How To Program Fifth Edition. Pearson Prentice Hall, 2007.