- Asked 4/16
- 2072 Chaitra · 2 marks
- 2073 Shrawan · 2 marks
- 2069 Asar · 2 marks
- 2068 Chaitra · 2 marks
Structure versus array
Short answer
A structure groups variables of different types under one name; an array holds elements of a single type. Structures are preferred when a record (e.g., student with name, roll, marks) mixes types, because they keep all fields of an entity together, allowing the whole record to be assigned, passed to functions, or returned as a unit.
How does a structure differ from an array? Why do we prefer structure over array?
A structure is a user-defined type that groups variables of different types under one name. An array is a collection of variables of the same type.
| Basis | Structure | Array |
|---|---|---|
| Data types | Members may have different types | All elements have the same type |
| Keyword | Declared with struct | No keyword |
| Access | By member name: s.roll | By index: a[2] |
| Memory | Members stored in order, may have padding bytes | Elements stored in consecutive memory without gaps |
| Type | User-defined data type | Derived data type |
| Pointer to element | Arrow operator p->x | Pointer arithmetic *(p+i) |
Why structure is preferred over an array
A record such as a student (name, roll, marks) mixes character, integer and float data, which an array cannot hold. A structure keeps all details of one entity together under one name, so they can be copied, passed to functions and returned as a unit. An array of structures can then store many records.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Structure vs array; accessing members · Computer Programming, chapter 8
- Pass student array; display passed students · Computer Programming, chapter 7
- Standard deviation and mean of array · Computer Programming, chapter 7
- Prefer O(n) or O(log n) algorithm · Data Structure and Algorithm, chapter 9
- Array representation vs dynamic list · Data Structure and Algorithm, chapter 3
- ADT versus data structure · Data Structure and Algorithm, chapter 1
- R-2R ladder DAC versus weighted resistor · Instrumentation I, chapter 5