Structures and Unions Flashcards
(5 cards)
What is a struct in C?
A user-defined data type that allows grouping variables of different data types under a single name, useful for representing records.
How do you access members of a structure variable using the dot operator (.)?
structureVariable.memberName
When accessing a structure member through a pointer to the structure, which operator do you use?
The arrow operator (->). E.g., structurePointer->memberName.
What is a union in C? How does it differ from a struct in terms of memory usage?
A user-defined data type where all its members share the same memory location. The size of a union is equal to the size of its largest member, unlike a struct which sums the sizes of its members. Only one member can hold a value at any given time.
When would you choose a union over a struct?
When you need to store only one of several possible data types at a time, to save memory.