- 2074 Chaitra · 4 marks
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
| Basis | Structure | Array |
|---|---|---|
| Element types | Different types allowed | Same type only |
| Declaration | struct keyword with members | type name[size] |
| Access | By member name | By index |
| Use | One record with many fields | A list of similar values |
Ways to access structure members
- Dot operator (.) with a structure variable:
s1.roll = 5; - Arrow operator (->) with a pointer to a structure:
ptr->roll = 5;, which is the same as(*ptr).roll. - Array of structures:
s[2].roll, using the index and then the dot. - Nested structures: dot repeated,
s1.dob.year.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Accessing array elements using pointer · Computer Programming, chapter 9
- Structure versus array · Computer Programming, chapter 8 · asked 4×
- Sending structure members to a function · Computer Programming, chapter 8
- Declaration vs definition; C program structure · Computer Programming, chapter 3
- Height difference using nested structure · Computer Programming, chapter 8
- College records with nested date structure · Computer Programming, chapter 8
- Access overridden members from derived class · Object Oriented Programming, chapter 6 · asked 2×
- Array representation vs dynamic list · Data Structure and Algorithm, chapter 3
- Multiplexing vs multiple access · Data Communication, chapter 6
- CDMA vs TDMA; FHSS multiple access · Wireless Communication, chapter 7