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 -> (arrow) operator is a member access operator used to dereference a pointer to a struct or union and access a specific member within it in a single operation. It serves as syntactic sugar for dereferencing the pointer with the indirection operator (*) and subsequently accessing the member using the dot operator (.).
pointer_expression->member_identifier

Semantic Equivalence

The arrow operator is strictly equivalent to enclosing the dereferenced pointer in parentheses and applying the dot operator. The parentheses are mandatory in the expanded form because the dot operator (.) has higher precedence than the indirection operator (*).
// These two expressions yield the exact same lvalue:
ptr->member;
(*ptr).member;

Operand Constraints

  1. Left Operand: Must be an expression of type “pointer to struct” or “pointer to union”. The structure or union type must be complete at the point of evaluation so the compiler knows the memory layout.
  2. Right Operand: Must be a valid identifier naming a member declared within the specific struct or union pointed to by the left operand.

Evaluation and Result

When evaluated, the compiler takes the base memory address held by the pointer, adds the byte offset of the specified member, and yields the resulting memory location.
  • Type: The type of the result is the declared type of the member, additionally qualified by any type qualifiers (const or volatile) applied to the struct or union type pointed to by the left operand. For example, accessing a non-const member through a const struct MyStruct * yields a const-qualified result. (Note: The restrict qualifier is not applicable here, as C dictates restrict can only qualify pointer types, not struct or union types).
  • Value Category: The result is always an lvalue (locator value), meaning it designates an object in memory. However, it is a modifiable lvalue (valid on the left side of an assignment) only if it meets all of the following conditions:
    • The resulting qualified type is not const-qualified.
    • The member is not an array type.
    • The member is not an incomplete type.
    • If the member is itself a struct or union type, it does not contain any const-qualified members (recursively including members or elements of all contained aggregates or unions).

Precedence and Associativity

The -> operator shares the highest precedence level in C (Level 1), alongside the dot operator (.), function call (), and array subscript [] operators. It evaluates with left-to-right associativity. This allows for sequential chaining when dealing with nested pointers to structures.
// Evaluates as: ((ptr->nested_ptr)->member)
ptr->nested_ptr->member; 

Memory Safety

The -> operator assumes the left operand contains a valid memory address. If the pointer is NULL, uninitialized, or points to freed memory, evaluating the -> operator invokes undefined behavior, typically resulting in a segmentation fault or memory access violation at runtime. The operator performs no bounds or null checking at the language level.
Master C with Deep Grasping Methodology!Learn More