Data Structures · Structures and Unions
Accessing Members with the Dot (Member) Operator
Overview
A structure definition creates several structure variables, each holding its own copy of every declared member.
The dot (member) operator is the syntax that selects one member of one particular structure variable, removing the ambiguity that arises when the same member name exists in several structures.
Key concepts
- Structure members: Writing a structure definition with the members listed inside the braces creates three structures, and each of these three structures has all the members listed in the definition.
Every structure therefore contains its own set of members. - Member operator (dot,
.): The operator used to access any member of any structure, written asstructure_variable.member_name. - Role of the dot: The member operator acts as the connection between the structure name (structure variable) and the structure member.
- Why the operator is necessary: A member such as the integer
iexists in three places (one copy inside each of the three structures).
Referring toialone is ambiguous; the structure name, dot and member name together identify exactly one member. - Repetition problem: Declaring another variable of the same structure type requires writing the entire definition (all the members) again, and nesting a structure inside another structure requires repeating the whole inner definition.
Writing this entire thing repeatedly is tedious; this motivates giving the structure a tag.
How it works
- The definition creates three structure variables, each holding all the members declared in the structure.
- To reach a particular member of a particular structure, write the structure variable name, then a dot, then the member name — for example
x.i. - The dot is the member operator; it links the structure name to the structure member.
- Because the member
iexists in three places, you must write the structure name, followed by a dot, followed by the member name (for examplex.i) to distinguish between them. - To declare or define another variable of the same structure type, the entire definition (all the members) must be written again, followed by the variable name.
- To define a structure that contains another structure inside it,
structand the inner members such asint cmust be written out again — the whole inner definition is repeated.
Formulas
structure_variable.member_name
structure_variable— the name of the structure variable, which tells which structure's member is wanted.— the member operatormember_name— the member inside that structure
x.i
x— the structure variable.— the member operatori— the member inside that structure
Worked examples
Example 1 — Accessing a member. Given structure variable x with a member i, write x.i: x selects which structure's member is wanted, . is the member operator, and i is the member inside that structure.
Example 2 — The ambiguity problem. The member i (an integer) is present in three places — one copy in each of the three structures created by the definition.
| Reference written | Meaning |
|---|---|
i | Ambiguous — which of the three i members? |
x.i | The member i of structure variable x (unambiguous) |
Example 3 — Repeating the definition (the tedious case). To declare one more variable of the same structure type, the whole definition must be rewritten:
struct {
int i;
/* ... all other members ... */
} y;
To define a structure that contains the earlier structure inside it, the inner members must again be written out, e.g. struct { ... int c; ... } — repeating everything is tedious.
Comparison
| Reference written | Meaning |
|---|---|
i | Ambiguous — which of the three i members? |
x.i | The member i of structure variable x (unambiguous) |
Revision summary
- A structure definition with three variables creates three structures, each having all the declared members.
- The dot (
.) is the member operator; syntax isstructure_variable.member_name, e.g.x.i. - The member operator connects the structure name with the structure member.
- The member operator is required because the same member name (e.g.
i) can exist in multiple structures; structure name + dot + member name removes the ambiguity. - Repeating the full structure definition to declare new variables, or to nest the structure inside another structure, is tedious — this motivates giving the structure a tag.
Previous-year exam insights
- The dot operator applies to a structure (or union) value/variable; for a pointer to a structure, member access uses the arrow operator p->member, which is equivalent to (*p).member.
Asked inGATE 2018 - Members of a structure are stored in the order declared, and the address of a member equals the base address of the structure plus the offset of that member (subject to padding/alignment); pointer arithmetic on a char* therefore walks byte-by-byte through the members.
Asked inGATE 2018 - The dot operator has higher precedence than most operators and associates left to right; it cannot be applied to a structure type name, only to a variable/value of that type.
- A structure tag (struct tag) lets the same type be reused for further variable declarations and for nesting without rewriting the member list; the tag names the type, while the dot selects a member of a variable.
- Unions differ from structures: all members share the same storage, so only one member is meaningfully active at a time and the union's size is that of its largest member (plus padding).
- Nested member access chains the dot operator, e.g. outer.inner.member, and each dot selects one level of the structure hierarchy.