Introduction to C / C++ Programming
Structures, Unions, and Enumerated Types

Structures

Declaring New Structure Types and struct variables

Tagged ( Named ) Structs

struct Part {
	int number, on_hand;
	char name [ NAME_LEN + 1 ];
	double price;
};
          struct Part p1, p2;

Use of typedef with structs

Scope of struct type definitions

Initializing variables of type struct

          Student john = { 3, "John Doe", 4.0 }, susie = { 5, "Susie Jones" }; // Susie has a gpa of 0.0
          Student jane = { .gpa = 4.0 }, sue = { .name = "Sue Smith", 3.5 }; // Sue has 0 classes and a gpa of 3.5

Accessing data fields within structs

          john.nClasses = 4;

          totalGPA += sue.gpa;

Assigning one struct to another

          joe = sue;      // Legal, both of the same type
          mary = carla;   // Also legal, both of the same type
          alice = bill;   // Still legal.  Both the same unnamed type

          alice = charlie;  // Illegal.  Different types, even though identical

Nested Structs

        struct Date {
            int day, month, year; };
            
        struct Exam {
            int room, nStudents;
            
            struct Date date;
            
            struct { 
            	int hour, minute;
                bool AM;
            } time;
            
            typedef struct Score{
            	int part1, part2, total; } Score;
                
            Score scores[ 100 ];
        };
        struct Exam CS107Exam2 = { 1000, 50, { 5, 4, 2013 }, { 6, 0, false } }; // All scores zero
            
        CS107Exam2.nStudents = 90;
        CS107Exam2.date.month = April;  // Assumes April is an int variable or constant, probably = 4.
        CS107Exam2.time.hour = 6;
        CS107Exam2.scores[ i ].part2 = 20;

Structs and Arrays

Structs and Functions

Structs and Pointers

Struct bit fields

Enumerated Data Types

Unions