Lecture 12 Structures 1 Outline Introduction Structure Definitions




























- Slides: 28

Lecture 12 - Structures 1

Outline Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array of Structures 2

Introduction Structures 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: Object Characteristics Book Car Student price, number of pages, year published price, year, model, colour name, matrix no, semester 3

Structure Definition Syntax: struct Structure. Type. Name { structure member declaration list }; Example: struct book { float price; int numpages; int year; }; 4

Structure Definition struct information A struct cannot 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 5

Structure Declaration After defining a structure type, we may declare variables that are of that type. A structure variable declaration requires these elements : keyword structure type name a list of variables names separated by commas concluding semicolon E. g. struct book 1; struct book my_book, his_book, her_book; 6

How does it look like in my program? #include <stdio. h> struct book { float price; int numpages; int year; }; struct book { float price; int numpages; int year; } book 1; OR int main( ) { { struct …… } book 1; …… } structbook 1; 7

Initializing Structures struct book 1 = {25. 50, 690, 2005}; OR book 1. price = 25. 50; book 1. numpages = 690; book 1. year = 2005; dot operator 8

Operations on Structures Members Operation Normal variable Structure member Read from user scanf(“%f”, &price); scanf(“%f”, &book 1. price); Assign value price = 25. 50; book 1. price = 25. 50; Print as output printf(“%fn”, price); printf(“%fn”, book 1. price); Copy to a variable newprice = price; newprice = book 1. price; Pass value to a function if(fun 1(price) > 5) if(fun 1(book 1. price) > 5) 9

Multiple Structures Variables struct book my_book, his_book, her_book; price numpages year my_book price numpages year his_book price numpages year her_book 10

Multiple Structures Variables e. g. struct book my_book, his_book, her_book; my_book. price = 25. 50; her_book. price = 10. 50; if(my_book. price > her_book. price) printf(“My book is more expensive than hersn”); else printf(“My book is cheaper than hersn”); 11

Sample Program #include <stdio. h> struct book { float price; int numpages; int year; }; int main() { struct book my_book = {25. 50, 690, 2005}; struct book her_book; printf("Enter book price : "); scanf("%f", &her_book. price); printf("Enter number of pages : "); scanf("%d", &her_book. numpages); printf("Enter year published : "); scanf("%d", &her_book. year); printf("My book : n"); printf("%. 2 ft%dn", my_book. price, my_book. numpages, my_book. year); printf("Her book : n"); printf("%. 2 ft%dn", her_book. price, her_book. numpages, her_book. year); if(my_book. year > her_book. year) printf("My book is the latest publicationn"); else printf("Her book is the latest publicationn"); return 0; } 12

Structures as Function Parameters Like variables of any other data type, 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. 13

Structures as Function Parameters …… float compute_price(struct book bk); //functionprototype int main() { struct book. C; …… new_price = compute_price(book. C); …… return 0; } //functioncall float compute_price(struct book bk. C) { …… bk. C. price=bk. C. price + tax; …… return(bk. C. price); } //functiondefinition 14

Structures as Function Parameters A nonvoid functionof 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 read (); int main( ) { ………. struct book b; b = read( ); ………. } //function prototype //function call struct book read () //function definition { struct book bk; printf(“Enter price: ”); scanf(“%f”, &bk. price); …. . return(bk); } 15

Structures as Function Parameters If all members of a structure variable are not needed for a function to perform its task, we canpass onlythe required members. However, we must specify structure members using the component selection operator. int modify_year(int a, int b, int year); //function prototype int main ( ) { struct book bk. C; …… avg_year=modify_year(aa, bb, bk. C. year); //function call ……. } 16

Structures as Function Parameters It is also possible to pass structure variables using pointers. int main() { struct book b; ……… read(&b); ……… } //function call void read(struct book *bk) { printf(“Enter price: ”); scanf(“%f”, &bk->price); printf(“Enter numpages: ”); scanf(“%d”, &bk->numpages); ……. } 17

struct book read() { struct book her_book; Sample Program #include <stdio. h> struct book { float price; int numpages; int year; }; struct book read(); void print(struct book, struct book); void compare(int, int); int main() { struct book my_book = {25. 50, 690, 2005}; struct book she_book; she_book=read(); print(my_book , she_book); compare(my_book. year, she_book. year); return 0; } } printf("Enter book price : "); scanf("%f", &her_book. price); printf("Enter number of pages : "); scanf("%d", &her_book. numpages); printf("Enter year published : "); scanf("%d", &her_book. year); return(her_book); void print(struct book my_book, struct book her_book) { printf("My book : n"); printf("%. 2 ft%dn", my_book. price, my_book. numpages, my_book. year); printf("Her book : n"); printf("%. 2 ft%dn", her_book. price, her_book. numpages, her_book. year); } void compare(int my_year, int she_year) { if(my_year > she_year) printf("My book is the latest publicationn"); else printf("Her book is the latest publicationn"); } 18

An Array of Structures Suppose a company has 50 full-time employees. struct employee. Type { char first. Name[20]; char last. Name[20]; int person. ID; char dept. ID[10]; double yearly. Salary; double monthly. Salary double year. To. Date. Paid; double monthly. Bonus; }; struct employee. Type employees[50]; - to declare for 100 employees' records: struct employee. Type employees[100]; 19

How it looks like 20

How to access? ? int counter; for(counter = 0; counter < 50; counter++) { scanf(“%s %s %d %s %lf”, &employees[counter]. first. Name, &employees[counter]. last. Name, &employees[counter]. person. ID, &employees[counter]. dept. ID, &employees[counter]. yearly. Salary); employees[counter]. monthly. Salary = employees[counter]. yearly. Salary/12; employees[counter]. year. To. Date. Paid = 0. 0; employees[counter]. monthly. Bonus = 0. 0; } 21

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 year. To. Date. Paid amount. The following loop computes and prints the employee’s paycheck for the month: doublepay. Check; //variable to calculate the paycheck for(counter = 0; counter < 50; counter++) { printf(“%s %s”, employees[counter]. first. Name, employees[counter]. last. Name); pay. Check = employees[counter]. monthly. Salary + employees[counter]. monthly. Bonus; employees[counter]. year. To. Date. Paid = employees[counter]. year. To. Date. Paid + pay. Check; } 22

Arrays in Structures Example const array. Size = 5; struct list. Type { int list. Elem[array. Size]; int list. Length; //array containing the list //length of the list }; struct list. Type list; 23

How it looks like struct list. Type list. Elem list. Length 24

Structure Within a Structure structname. Type { string first; string middle; string last; }; structaddress. Type { string address 1; string address 2; string city; string state; string zip; }; structdate. Type { string month; string day; string year; }; structcontact. Type { string phone; string cellphone; string fax; string pager; string email; }; structemployee. Type { structname. Typename; string empl. ID; structaddress. Typeaddress; structdate. Typehiredate; structdate. Typequitdate; structcontact. Typecontact; string dept. ID; double salary; }; 25

Structure within a Structure: How to access? ? //variab le declaration struct employee. Type new. Employee; Example: new. Employee. salary = 45678. 00; new. Employee. name. first = "Mary"; new. Employee. name. middle = "Beth"; new. Employee. name. last = "Simmons"; 26

Structure Within a Structure : How to access? ? The statement scanf(“%s”, &new. Employee. name. first); reads and stores a string into new. Employee. name. first. The statement new. Employee. salary = new. Employee. salary * 1. 05; updates the salary of new. Employee 27

End – Structures Q & A! 45