Chapter 10 Summary and Quiz
Recap of structures, unions, and bit-fields.
Your Own Types
This chapter handed you the keys to C's type system: structs group what belongs together, arrays of structs are the tables real programs keep, functions traffic in whole records, and unions with bit-fields cover the specialist corners. One rule ran through everything, group what moves together, and one preview kept recurring: addresses fix the copying costs, next chapter. The recap.
Structs
struct Tag { members }; defines a type named struct Tag, both words, bare tags are C++'s habit, with typedef struct Tag { ... } Name; as the sanctioned sugar. Variables may ride along with the definition (} origin, corner;), tagless if you insist and then unnameable forever, while a member is never initialized inside the template. The dot selects members and outranks every arithmetic and relational operator; brace initializers fill in order and zero the rest; nested structs chain dots. Structs assign whole (b = a, independent copies), the courtesy arrays never had, but never compare with ==: equality is member by member, and memcmp is wrong because padding, the alignment gaps that make a struct's size exceed its members' sum, holds indeterminate bytes. Sizes come from sizeof, always.
Tables of Records
struct Student students[N]: index first, member second (students[i].marks), &students[i].roll scans into a record, nested braces initialize with the zero-the-rest rule, and the chapter 7 capacity/count discipline applies unchanged. A member can itself be an array, which is where students[i].marks[j] gets its two subscripts, table then element, and sizeof(students) / sizeof(students[0]) counts the records of any table still in scope. Searches track the index of the winning record; sorts compare one member but swap whole structs through a struct temp; and against parallel arrays the design argument stands: reordering one array decouples every record, while structs weld members together.
Records and Functions
Struct parameters copy completely (caller untouched); struct returns travel whole into caller assignment, return-and-assign with records. The cost column: small structs move by value gracefully, large ones copy their full size per call, the exam's "drawback" whose fix is address-passing. Structs copy even the arrays inside them, while bare arrays never copy: pointers reconcile the pair next chapter. File shape: types above functions above main.
Unions and Bit-Fields
A union overlays all members at one address: sized by the largest, one meaningful value at a time, and reading a member other than the last written reinterprets bytes. Its brace initializer always lands on the first member, converting whatever value you wrote to that member's type, so every other member is set by assignment afterwards. The tagged union, a struct pairing a tag with the union, checked before every access, is the safe idiom and the exam answer. Bit-fields pack struct members into bits (unsigned int flag : 1), truncating oversized stores, addressless, un-arrayable, and implementation-defined in layout, with unnamed fields (unsigned int : 2;) reserving unreachable bits and an over-wide field moving to the next storage unit rather than straddling on this platform (implementation-defined, like the rest of the layout): internal flags yes, portable formats no.
One Program, Whole Chapter
A record table, a struct-in-struct-out function, whole-struct assignment accumulating a winner, and a tie resolved by the >= keeping the earlier record: predict the output, run it, take the quiz.
Looking Ahead
Chapter 11 is the one the whole course has been building toward: pointers. The & you have typed since chapter 1, the arrays that pass without copying, the swap that could not work, the large structs too big to copy: every IOU comes due, and C's most feared topic arrives with every prerequisite already in your hands.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Chapter 10 Summary and Quiz - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!