320 likes | 589 Views
Structures. Outline. Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array of Structures. Introduction: What is a structure?.
E N D
Structures PGT 106 C Programming
Outline • Introduction • Structure Definitions and Declarations • Initializing Structures • Operations on Structures Members • Structures as Functions Parameters • Array of Structures PGT 106 C Programming
Introduction:What is a structure? • Collection of related data items called components (or members) that are NOT necessarily of the same data type. • Commonly used to define records to be stored in files. • Usually the collections of related data item are characteristics in an object • For example: ObjectCharacteristics Book price, number of pages, year published Car price, year, model, colour Student name, matric_no, semester PGT 106 C Programming
Introduction:What is a structure? • Collection of related data • Data = components = members • Referred to using a same name • But the data/component/member can be of different types
Example 1 STUDENT1 Student Name Age STUDENT2 Matric Number Nationality CGPA Total Credit Hours Taken
Example 2 MyBook BOOK Price HisBook Number of Pages Year Published HerBook
Structure Definition • Syntax: • struct StructureTypeName { structure member declaration list }; Example: struct book { float fPrice; int iNumPages; int iYear; }; DEFINE PGT 106 C Programming
Take Note!! • struct information • A structcannot contain an instance of itself • Can contain a member that is a pointer to the same structure type • A structure definition does not reserve space in memory • Instead creates a new data type used to define structure variables PGT 106 C Programming
Structure Declaration • After defining, we may declare variables that are of that type, with these elements : • keyword struct • structure type name • a list of variables names separated by commas • concluding semicolon E.g. struct student sStudent1; struct book sMyBook,sHisBook,sHerBook; PGT 106 C Programming
How does it look like in my program? #include <stdio.h> struct book { float fPrice; int iNumPages; int iYear; }; int main( ) { struct book sBook1; …… } #include <stdio.h> struct book { float fPrice; int iNumPages; int iYear; } sBook1; int main( ) { …… } or struct book sBook1; PGT 106 C Programming
Initializing Structures struct book sBook1 = {25.50, 690, 2005}; OR sBook1.fPrice = 25.50; sBook1.iNumPages = 690; sBook1.iYear = 2005; dot operator PGT 106 C Programming
Operations on Structures Members PGT 106 C Programming
Multiple Structures Variables struct book sMyBook, sHisBook, sHerBook; • fPrice • fPrice • fPrice • iNumPages • iNumPages • iNumPages • iYear • iYear • iYear sMyBook sHisBook sHerBook PGT 106 C Programming
Multiple Structures Variables e.g. struct book sMyBook, sHisBook, sHerBook; sMyBook. fPrice = 25.50; sHerBook.fPrice = 10.50; if(sMyBook.fPrice > sHerBook.fPrice) printf(“My book is more expensive than hers\n”); else printf(“My book is cheaper than hers\n”); PGT 106 C Programming
Sample Program printf("My book :\n"); printf("%.2f\t%d\t%d\n", sMyBook.fPrice, sMyBook.iNumPages, sMyBook.iYear); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", sHerBook.fPrice, sHerBook.iNumPages, sHerBook.iYear); if(sMyBook.iYear > sHerBook.iYear) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); return 0; } #include <stdio.h> struct book { float fPrice; intiNumPages; intiYear; }; int main() { struct book sMyBook = {25.50,690,2005}; struct book sHerBook; printf("Enter book price : "); scanf("%f", &sHerBook.fPrice); printf("Enter number of pages : "); scanf("%d", &sHerBook.iNumPages); printf("Enter year published : "); scanf("%d", &sHerBook.iYear); PGT 106 C Programming
Structures as Function Parameters • structure variables can be used as formal and actual function parameters. • In passing structure variables by value to a function, the operating systems makes a copy of the entire structure in the data area of the called function. PGT 106 C Programming
Structures as Function Parameters …… float fnComputePrice(struct book sBkC);//function prototype int main( ) { ……….. struct book sBookC; …………… fNewPrice = fnComputePrice(sBookC); //function call …… } float fnComputePrice(struct book sBkC) //function definition { ……. sBkC.fPrice=sBkC.fPrice + fTax; …… return(fBkC.fPrice); } PGT 106 C Programming
Structures as Function Parameters • A nonvoid function of a structure type can return a structure of that structure type under the function’s name if we use a return statement in the function. struct book fnRead (); //function prototype int main( ) { ………. struct book sB; sB = fnRead( ); //function call ………. } struct book fnRead( ) //function definition { struct book sBk; printf(“Enter price:”); scanf(“%f”, &sBk.fPrice); ….. return(sBk); } PGT 106 C Programming
Structures as Function Parameters • If all members of a structure variable are not needed for a function to perform its task, we can pass only the required members. • However, we must specify structure members using the component selection operator. int fnModifyYear(int iA, int iB, int iYear); //function prototype …….. int main( ) { struct book sBkC; ……………. iAvgYear=fnModifyYear(iAa, iBb, sBkC.iYear); //function call ……. } PGT 106 C Programming
Structures as Function Parameters • It is possible to pass structure variables using pointers int main() { struct book sB; ……… fnRead(&sB); //function call } void fnRead(struct book *sBk) { printf(“Enter price:”); scanf(“%f”, &sBk->fPrice); printf(“Enter numpages:”); scanf(“%d”, &sBk->iNumPages); ……. } PGT 106 C Programming
Sample Program #include <stdio.h> struct book { float fPrice; int iNumPages; int iYear; }; struct book fnRead(); void fnPrint(struct book, struct book); void fnCompare(int, int); int main() { struct book sMyBook = {25.50,690,2005}; struct book sSheBook; sSheBook=fnRead(); fnPrint(sMyBook , sSheBook); fnCompare(sMyBook.iYear, sSheBook.iYear); return 0; } struct book fnRead() { struct book sHerBook; printf("Enter book price : "); scanf("%f", &sHerBook.fPrice); printf("Enter number of pages : "); scanf("%d", &sHerBook.iNumPages); printf("Enter year published : "); scanf("%d", &sHerBook.iYear); return(sHerBook); } void fnPrint(struct book sMyBook, struct book sHerBook) { printf("My book :\n"); printf("%.2f\t%d\t%d\n", sMyBook.fPrice, sMyBook.iNumPages, sMyBook.iYear); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", sHerBook.fPrice, sHerBook.iNumPages, sHerBook.iYear); } void fnCompare(int iMyYear, int iSheYear) { if(iMyYear > iSheYear) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); } PGT 106 C Programming
An Array of Structures • Suppose a company has 50 full-time employees. struct employeeType { char acFirstName[20]; char acLastName[20]; int iPersonID; char acDeptID[10]; double dYearlySalary; double dMonthlySalary; double dYearToDatePaid; double dMonthlyBonus; }; struct employeeType asEmployees[50]; PGT 106 C Programming
How it looks like Array of structs asEmployees PGT 106 C Programming
How to access?? int iCounter; for(iCounter = 0; iCounter < 50; iCounter++) { scanf(“%s %s %d %s %lf”, &asEmployees[iCounter].acFirstName, &asEmployees[iCounter].acLastName, &asEmployees[iCounter].iPersonID, &asEmployees[iCounter].acDeptID, &asEmployees[iCounter].dYearlySalary); asEmployees[iCounter].dMonthlySalary = asEmployees[iCounter].dYearlySalary/12; asEmployees[iCounter].dYearToDatePaid = 0.0; asEmployees[iCounter].dMonthlyBonus = 0.0; } PGT 106 C Programming
How to access?? • Suppose that for a given month the monthly bonuses are already stored in each employee’s record, and we have to calculate the monthly paycheck and update the yearToDatePaid amount. The following loop computes and prints the employee’s paycheck for the month: double dPayCheck; //variable to calculate the paycheck for(iCounter = 0; iCounter < 50; iCounter++) { printf(“%s %s”, asEmployees[iCounter].acFirstName, asEmployees[iCounter].acLastName); dPayCheck = asEmployees[iCounter].dMonthlySalary + asEmployees[iCounter].dMonthlyBonus; asEmployees[iCounter].dYearToDatePaid = asEmployees[iCounter].dYearToDatePaid + dPayCheck; } PGT 106 C Programming
Arrays in Structures • Example const arraySize = 5; struct listType { int aiListElem[arraySize]; //array //containing the list int iListLength; //length of // the list }; struct listType sList; PGT 106 C Programming
How it looks like • struct listType aiListElem iListLength PGT 106 C Programming
Structure Within a Structure struct nameType { string acFirst; string acMiddle; string acLast; }; struct addressType { string acAddress1; string acAddress2; string acCity; string acState; string acZip; }; struct dateType { string acMonth; string acDay; string acYear; }; struct contactType { string acPhone; string acCellphone; string acFax; string acPager; string acEmail; }; struct employeeType { struct nameType sName; string acEmplID; struct addressType sAddress; struct dateType sHiredate; struct dateType sQuitdate; struct contactType sContact; string acDeptID; double dSalary; }; PGT 106 C Programming
Structure Within a Structure : How to access?? //variable declaration struct employeeType sNewEmployee; //declare 100 employees' records struct employeeType asEmployees[100]; sNewEmployee.dSalary = 45678.00; sNewEmployee.sName.acFirst = "Mary"; sNewEmployee.sName.acMiddle = "Beth"; sNewEmployee.sName.acLast = "Simmons"; PGT 106 C Programming
Structure Within a Structure : How to access?? The statement scanf(“%s”, &sNewEmployee.sName.acFirst); reads and stores a string into sNewEmployee.sName.acFirst. The statement sNewEmployee.dSalary = sNewEmployee.dSalary * 1.05; updates the salary of newEmployee PGT 106 C Programming
TQ PGT 106 C Programming