1 / 18

Structures

Structures. Hugo Bento Jonathan Briceno Miguel Morales Sergio Naser. Definition of a Structure:.

mareo
Télécharger la présentation

Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures Hugo Bento Jonathan Briceno Miguel Morales Sergio Naser

  2. 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.

  3. 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]; };

  4. 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.

  5. 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

  6. 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);

  7. 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”};

  8. Example with Visual Aid • struct person { string name; inteye_colour; float height; }; person friend,mother; person spouse;

  9. Accessing Members of Structures • Structure member operator/ dot operator (.) • Structure pointer operator / arrow operator (->) • rec.fname • rec.lname • (*cardPtr).face • rec->fname • rec->lname

  10. 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;

  11. 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);

  12. Output Structure Array

  13. 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);

  14. Output Structure Array (Pointer)

  15. 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

  16. 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);

  17. Example Output From Book:

  18. 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.

More Related