Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The . (dot) operator, formally known as the direct member access operator, is a binary operator used to access the individual members of a struct or union object. It evaluates to the value of the specified member and yields the type of that specific member.
operand1.operand2

Operand Constraints and Semantics

  • Left Operand (operand1): Must be an expression that evaluates to a complete struct or union type. It cannot be a pointer to a struct or union; pointer types require the indirect member access operator (->).
  • Right Operand (operand2): Must be a valid identifier that explicitly names a member declared within the struct or union type of the left operand.
  • Atomic Semantics (C11): The constraints of the C standard explicitly allow the left operand to have an _Atomic-qualified struct or union type. However, the semantic rules dictate that evaluating the . operator on an atomic struct or union object results in undefined behavior.

Value Categories and Qualifiers

The value category of the expression resulting from the . operator depends entirely on the left operand:
  • Lvalue/Rvalue: If the left operand is an lvalue (e.g., an instantiated variable), the result is an lvalue. If the left operand is an rvalue (e.g., a struct returned by value from a function), the result is an rvalue.
  • Modifiable Lvalue: An lvalue result is a modifiable lvalue (capable of being assigned to) only if all the following conditions are met:
    • The member is not an array type.
    • The left operand is not const-qualified.
    • The specific member was not declared with the const qualifier.
    • Recursive const Rule: If the accessed member is itself a struct or union, it must not contain any const-qualified members, evaluated recursively through all nested aggregates or unions. (This rule has existed since C90).
  • Type Qualifiers: The resulting expression inherits the type qualifiers of the left operand. If the struct or union object is const-qualified or volatile-qualified, the accessed member is treated as const or volatile respectively, in addition to any qualifiers explicitly applied to the member’s original declaration.
  • Bit-fields: If the right operand designates a bit-field member, the . operator yields an lvalue (provided the left operand is an lvalue), but C semantics dictate that the address-of operator (&) cannot be applied to this resulting bit-field.

Precedence and Associativity

  • Precedence: The . operator possesses the highest precedence in C (Level 1, Postfix operators). It shares this exact precedence tier with the indirect member access (->), function call (()), array subscript ([]), postfix increment (++), and postfix decrement (--) operators.
  • Associativity: It evaluates strictly from left to right. This allows for direct chaining when dealing with nested structures.
// Syntax visualization of left-to-right associativity
structA.structB.memberC

// Evaluates as:
(structA.structB).memberC

Underlying Mechanics

At compile time, the C compiler resolves the . operator by calculating a fixed byte offset for the member, accounting for architecture-specific alignment padding.
  • Address Calculation: During execution, the compiler determines the member’s location by adding this constant offset to the base address or location of the struct or union.
  • Non-Addressable Objects: If the left operand is an rvalue or declared with the register storage class, the C standard prohibits taking its address using the & operator. The object may physically reside in memory (such as on the stack) or in hardware registers; regardless of physical placement, the compiler logically extracts the member’s value based on its structural offset.
  • Runtime Overhead: While the member’s offset is resolved entirely at compile time, accessing a member at a non-zero offset may still require CPU instructions for address calculation at runtime. If the offset is too large to fold into a single instruction’s addressing mode, the CPU must execute additional instructions to compute the final effective address.
Master C with Deep Grasping Methodology!Learn More