Skip to main content

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.

BasisStructureArray
Data typesMembers may have different typesAll elements have the same type
KeywordDeclared with structNo keyword
AccessBy member name: s.rollBy index: a[2]
MemoryMembers stored in order, may have padding bytesElements stored in consecutive memory without gaps
TypeUser-defined data typeDerived data type
Pointer to elementArrow operator p->xPointer 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)

All Computer Programming chapter 8 questions →