Skip to main content

Structure vs array; accessing members

Short answer

A structure groups different-type members, an array holds same-type elements. Access members via dot operator (s.member) with a variable, arrow operator (p->member) with a pointer, indexing then dot (arr[i].member) for an array of structures, and chained dots for nested members.

How does a structure differ from an array? What are the different ways to access structure member?

Structure versus array

BasisStructureArray
Element typesDifferent types allowedSame type only
Declarationstruct keyword with memberstype name[size]
AccessBy member nameBy index
UseOne record with many fieldsA list of similar values

Ways to access structure members

  1. Dot operator (.) with a structure variable: s1.roll = 5;
  2. Arrow operator (->) with a pointer to a structure: ptr->roll = 5;, which is the same as (*ptr).roll.
  3. Array of structures: s[2].roll, using the index and then the dot.
  4. Nested structures: dot repeated, s1.dob.year.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 8 questions →