170 likes | 381 Views
Erick Barrera Manuel Costa Marbelys Iglesias Adrian Martin Osvaldo Reyes Emmanuel Stimphil Elie Victor. EEL 2880 Group #4. Step thru Array of Structures. General Objectives: Explain and provide examples of how to pass through an array of structures using pointers
E N D
Erick Barrera Manuel Costa Marbelys Iglesias Adrian Martin Osvaldo Reyes Emmanuel Stimphil Elie Victor EEL 2880 Group #4
Step thru Array of Structures General Objectives: • Explain and provide examples of how to pass through an array of structures using pointers • Develop elements required to accomplish this • Visually illustrate the process through the use of a Block Diagram/Flow Chart • Describe how the program operates both overall and in depth • Provide alternate example
Introduction • What is a structure? • Structures, commonly referred to as aggregates, are collections of related variables under one name. • Structures may contain variables of many different data types, unlike arrays which can only hold elements of the same data type. • Pointers and structures facilitate the formation of more complex data structures such as linked lists, queues, stacks, and trees.
Structures That Contain Arrays You can define a structure that contains one or more arrays as members. The array can be of any C data type (int, char, and so on). For example, the statements struct data{ int x[4]; char y[10]; }; struct data record;
Structures That Contain Arrays You access individual elements of arrays that are structure members using a combination of the member operator and array subscripts: record.x[2] = 100; record.y[1] = ‘x’; Character arrays are most frequently used to store strings. Also, the name of an array, without brackets, is a pointer to the array. Because this holds true for arrays that are structure members, the expression record.v puts(record.v);
/* Demonstratessteppingthroughanarray of structures */ /* using pointer notation. */ #include <stdio.h> #define MAX 4 Compile Library: Standard Input/Output Library /* Define a structure, then declare and initialize */ /* anarray of 4 structures. */ structpart { intnumber; charname[10]; }; Structure is Declared and Created: one character array named “name” with 10 characters max. /* Declare a pointer totypepart, and a counter variable. */ structpart data[MAX] = { {1, "Smith"}, {2, "Jones"}, {3, "Adams"}, {4, "Wilson"} }; Initialize /* Declare a pointer totypepart, and a counter variable. */ structpart *p_part; intcount; Give Structure a name: Gives a place to store any data given by user and a way to reference it. intmain(void) { /* Initializethe pointer tothefirstarrayelement. */ p_part = data; /* Loopthroughthearray, incrementingthe pointer */ /* witheachiteration. */ for (count = 0; count < MAX; count++) { printf("\nAtaddress %p: %d %s", p_part, p_part->number, p_part->name); p_part++; } } Main Function
Lessons Learned • How to use pointers through an array of structures • Provide examples of how they can be used • The relationship between pointers and arrays • The step-by-step procedure of how the program works
Research • www.google.com • C-How to Program 5th & 6th Edition, Deitel& Deitel
Thank you. The End