180 likes | 318 Views
Structures. Hugo Bento Jonathan Briceno Miguel Morales Sergio Naser. Definition of a Structure:.
E N D
Structures Hugo Bento Jonathan Briceno Miguel Morales Sergio Naser
Definition of a Structure: • The array can be used to represent a group of related data item that belong to the same datatype. If we want to represent a collection of data items of different data types, using a single name, C supports a constructed datatype known as a structure. • Collections of related variables under one name. • May contain variables of many different data types.
Structure Definition: • structintroduces structure definition • donor is the structure tag • Variables within braces are members of the structure • Members can have a variety of data types struct donor{ float amount; char fname[30]; char lname[30]; };
Comparison of Structure Variables: • Two variables from the same structure tube can be compare the same way as ordinary variables. S1 = S2 assigns S1 to S2 • Structures may not be compared using operator s because structure members are not necessarily stored in consecutive bytes of memory.
Program FlowChart START Structure declaration Struct donor *rec rec=rec1 scanf (“%s %s”, rec->fname, rec->lname printf (“separate by a space:”) printf (“enter the donor ‘s first and last names,\n”) printf (\nDonor %s %s gave $%.2f.”, rec->fname, rec->lname,) scanf (“%f, rec->amount) printf (“\nEnter the Donation Amount: “) STOP
Outline of the Program • Define and declare a structure to hold the data • Struct donor { float amount; char fname [30]; char lname [30]; } • Declare new structure and Input the data from the keyboard. • struct donor rec; • printf(“Enter the donor’s first and last names,\n”); • printf(“separated by a space: “); • scanf(“%s %s”, rec.fname, rec.lname); • printf(“\nEnter the donation amount: “); • scanf(“%f, &rec.amount); • Display the information • printf (“\nDonor %s %s gave $%.2f.”, rec.fname, rec.lname, rec.amount);
Initializing Structures • Initializes variable rec to be of type struct donor • Members automatically initialized to zero or null • Initializes variable aCard to be of type struct card • Initializes member face to “Three” and suit to “Hearts” struct donor rec; Struct card aCard = {“Three”, “Hearts”};
Example with Visual Aid • struct person { string name; inteye_colour; float height; }; person friend,mother; person spouse;
Accessing Members of Structures • Structure member operator/ dot operator (.) • Structure pointer operator / arrow operator (->) • rec.fname • rec.lname • (*cardPtr).face • rec->fname • rec->lname
Example with Visual Aid 2 friend.name = "Diane"; friend.eye_colour = 1; friend.height = 1.61; mother.name = "Mary"; mother.eye_colour = 2; mother.height = 1.44; spouse.name = "Helen"; spouse.eye_colour = 1; spouse.height = 1.7;
Structure Array scanf("%s %s", rec.fname, rec.lname); scanf("%f", &rec.amount); printf("\nDonor %s %s gave $%.2f.", rec.fname, rec.lname, rec.amount);
Summary of Structure Array (Pointer) scanf("%s %s", rec->fname, rec->lname); scanf("%f", &rec->amount); printf("\nDonor %s %s gave $%.2f.", rec->fname, rec->lname, rec->amount);
Example Definition: • In main: • //card struct definition • struct card{ • char *face; //define pointer face • char *suit; //define pointer suit • }; //end structure card • struct card aCard; //define struct card variable • struct card *cardPtr; //define pointer to struct card
Example Initializer: • In main: aCard.face = "Ace"; aCard.suit = "Spades"; cardPtr = &aCard; //assigns address of aCard to cardPtr printf("%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit, cardPtr->face, " of ", cardPtr->suit, (*cardPtr).face, " of ", (*cardPtr).suit);
Conclusion • Structures work much like arrays in terms of retrieving information. • There are advantages like being able to use different data types in a structure instead of just one type in arrays.