1 / 19

Structures, Unions, and Enumerations

Structures, Unions, and Enumerations. Structure Variables. The properties of a structure are quite different from those of an array. The elements of a structure aren’t required to have the same type. Declaring Structure Variables.

gordy
Download Presentation

Structures, Unions, and Enumerations

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, Unions, and Enumerations

  2. Structure Variables • The properties of a structure are quite different from those of an array. • The elements of a structure aren’t required to have the same type.

  3. Declaring Structure Variables • It is logical to organize a collection of related data items into a structure. struct{ int number; char name[NAME_LEN+1]; int on_hand; } part1, part2; struct {….} specifies a type

  4. Sturcture in Memory 2000 2001 number name on_hand

  5. Initializing Structure Variables struct{ int number; char name[NAME_LEN+1]; int on_hand; } part1 = {528, “Disk drive”, 10}, part2 = {914, “printer cables”, 5};

  6. Operations on Structure printf(“part number: %d\n”, part1.number); printf(“part name: %s\n”, part1.name); printf(“quantity on hand: %d\n”, part1.on_hand); part1.number = 258; part1.on_hand++; scanf(“%d”, &part1.on_hand);

  7. Structure Assignment part2 = part1; part2.number = part1.number; part2.name = part1.name; (*) part2.on_hand = part1.on_hand; struct { int a[10]; } a1, a2; a1 = a2; /*legal*/ But we can’t use the == and != operators

  8. Structure Types struct node{ int number; char name[NAME_LEN+1]; int on_hand; } part1; Repeating the structure information will bloat the program. Either declare a “structure tag” or use typedef to define a type name • struct node{ • int number; • char name[NAME_LEN+1]; • inton_hand; • } part2;

  9. Declaring a Structure Tag • A structure tag is a name used to identify a particular kind of structure. struct part{ int number; char name[NAME_LEN+1]; int on_hand; }; Should not be omitted

  10. Declaring a Structure Tag struct part part1, part2; part part1, part2; // WRONG struct part part1 = {52, “Disk drive”, 10}; struct part part2; part2 = part1;

  11. Defining a Structure Type typedef struct{ int nubmer; char name[NAME+1]; int on_hand; } Part; Part part1, part2;

  12. Structure as Arguments and Return Values print_part(struct part p) { printf(“part number: %d\n”, part1.number); printf(“part name: %s\n”, part1.name); printf(“quantity on hand: %d\n”, part1.on_hand); } print_part(part1);

  13. Structure as Arguments and Return Values struct part build_part(int number, const char *name, int on_hand) { struct part p; p.number = number; strcpy(p.name, name); p.on_hand = on_hand; return p; }

  14. Nested Arrays and Structures struct person_name{ char first[FIRST_NAME_LEN+1]; char middle_initial; char last[LAST_NAME_LEN+1]; } struct student{ struct person_name name; int id, age; char sex; }student1, student2;

  15. Nested Arrays and Structures strcpy(student1.name.first, “Fred”); display_name(student1.name);

  16. Array of Structures struct part inventory[100]; struc dialing_code{ char *country; int code; } const struct dialing_code country_codes[] = {{“Argentina”, 54}, {“Brazil”, 55}, …, {“U.S”, 1}, {“Vietnam”, 84} };

  17. Unions union{ int i; double d; } u;

  18. Enumerations • enum suit {CLUBS, DIAMONS, HEARTS, SPADES}; • enum suit s1, s2; • typedef enum {CLUBS, DIAMONS, HEARTS, SPADES} Suit; • Suit s1, s2;

  19. Enumerations as Integers • enum suit {CLUBS=1, DIAMONDS=2, HEARTS=3, SPADES=4}; • enum dept {RESEARCH=20, PRODUCTION=10, SALES=25}; • enum EGA_colors {BLACK, LT_GRAY=7, DK_GRAY, WHITE=15}; int i = DIAMONDS; Suit s = 0; s++; i = s + 2;

More Related