Structures 1 Structure Basics n n A structure
Structures 1
Structure Basics n n A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members can be of different types. 2
Structure Definition struct name { variable declaration; . . }; the keyword struct announces that this is a structure definition 3
Initializing Structures struct Rect { double x; double y; char color; double width; double height; }; struct Rect r 1 = {0, 0, ’r’, 5, 10}; r 1 0 x 0 y r color 5. 0 10. 0 width height 4
Accessing Data Members n To access a data member use n n Varname. membername struct Rect r 1 = {0, 0, ’r’, 5, 10}; n n n r 1. x r 1. y r 1. color r 1. width r 1. height r 1 0 x 0 y r color 5 width height 10 5
Assignment operator n Assignment operator is defined for structure of the same type. struct rect { double x; double y; char color; double width; double height; }; struct rect r 1, r 2 r 1. x = 10. 5; r 1. y = 18. 99; r 1. width = 24. 2; r 1. height = 15. 9; r 1. color = 'r'; /* Copy all data from r 1 to r 2. */ r 2 = r 1; 6
Scope of a Structure n n Member variables are local to the structure. Member names are not known outside the structure. 7
Exercise n Write a program using structures that manipulates pairs. Addition and multiplication of pairs is defined below. (a, b)+(c, d)=(a+c, b+d) (a, b)*(c, d)=(a*c, b*d) 8
Pair Structure n Store two integers to represent the first and second number of pair struct pair { int first; int second; }; 9
Addition struct pair add(struct pair p 1, struct pair p 2) { struct pair temp; temp. first = p 1. first + p 2. first; temp. second = p 1. second + p 2. second; return temp; } 10
Multiplication struct pair multiply(struct pair p 1, struct pair p 2) { struct pair temp; temp. first = p 1. first * p 2. first; temp. second = p 1. second * p 2. second; return temp; } 11
How to use the functions struct pair mp 1, mp 2, mp 3, mp 4; printf("Enter first pairn"); scanf("%d %d", &mp 1. first, &mp 1. second); printf("Enter second pairn"); scanf("%d %d", &mp 2. first, &mp 2. second); mp 3 = add(mp 1, mp 2); printf("Addition result = (%d, %d)n", mp 3. first, mp 3. second); mp 4 = multiply(mp 1, mp 2); printf("Multiplication result = (%d, %d)n", mp 4. first, mp 4. second); 12
Exercise n Update the program to support the following on pairs c*(a, b) = (c*a, c*b) (a, b)^c = (a^c, b^c) 13
Arrays of Structures n Arrays of structures may be declared in the same way as other C data types. struct rectangles[20]; n rectangles[0] references first structure of rectangles array. rectangles[0]. color = ‘r’; x y color width height ………. . . . 0 1 2 3 19 14
Structures as Arguments to Functions n When a structure is passed as an argument to a function, it is a call-by-value reference. n n Changes made to the formal parameters do not change the argument. A pointer to a structure may also be passed as an argument to a function. n Changes made to the formal parameters also change the argument. 15
Call by Value Example struct simple { int ival; double dval; }; void fun 1(struct simple s) { s. ival = s. ival+1; s. dval = s. dval + 2; } s s 1 10 10 ival 1. 5 dval int main(void) { struct simple s 1 = {10, 1. 5}; fun 1(s 1); printf(“%i %lfn”, s 1. ival , s 1. dval ); return 0; } Updated s 11 3. 5 16
Nested Structures Structure definitions may contain data members that are other structures: Card struct Card { char suit; int rank; }; struct Deck { struct Card cards[52]; int next_card = 0; }; suit rank 0 1 51 …. . . …… cards next_card 17
Initialize the Deck for (i=0; i<13; i++) { d 1. cards[i]. suit = 'c'; d 1. cards[i]. rank = i+1; } for (i=13; i<26; i++) { d 1. cards[i]. suit = 'd'; d 1. cards[i]. rank = (i-13)+1; } for (i=26; i<39; i++) { d 1. cards[i]. suit = 'h'; d 1. cards[i]. rank = (i-26)+1; } for (i=39; i<52; i++) { d 1. cards[i]. suit = 's'; d 1. cards[i]. rank = (i-39)+1; } 18
Print the Deck void print(struct Deck d 1) { int i; for (i=0; i<52; i++) printf("%c %dn", d 1. cards[i]. suit, d 1. cards[i]. rank); } return; 19
How to Shuffle the Deck? 0 1 2 3 …………… 51 for (i=0; i<52; i++) { // pick a random card x from 0 -51 // swap card x and card i } 20
Black. Jack n n Shuffle the deck Deal first 2 cars to user n n Next two cards to dealer n n n Print both cards on screen Print only the first on screen Ask user whether he/she wants to continue Highest total <= 21 wins n n n Jack, Queen, King are 10 points Ace is 11 points Other cards represented by their number 21
- Slides: 21