Structures and Unions Flashcards

(5 cards)

1
Q

What is a struct in C?

A

A user-defined data type that allows grouping variables of different data types under a single name, useful for representing records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you access members of a structure variable using the dot operator (.)?

A

structureVariable.memberName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When accessing a structure member through a pointer to the structure, which operator do you use?

A

The arrow operator (->). E.g., structurePointer->memberName.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a union in C? How does it differ from a struct in terms of memory usage?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When would you choose a union over a struct?

A

When you need to store only one of several possible data types at a time, to save memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly