Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Member access operators . and -> in C

Reference for the C member access operators: the dot for structs and unions, the arrow for pointers, chaining, precedence and nested members.

C has two operators for reaching a member of a structure or union. The dot, obj.member, works on the object itself. The arrow, ptr->member, works on a pointer to the object and is defined as shorthand for (*ptr).member.

Both are postfix operators with the highest precedence in the language, so they bind tighter than *, &, ++ and every binary operator. That is why *p.x does not mean what newcomers expect, and why the arrow exists.

C
struct point { int x, y; };
struct point a = {1, 2};
struct point *p = &a;
a.x = 10;       /* through the object */
p->y = 20;      /* through the pointer */

Try it

C Dot and arrow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Nested members, unions and chains

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
postfix-expression . identifier
postfix-expression -> identifier
Form Left operand Result
s.m A structure or union (an lvalue or a value returned by a function). The member m. It is an lvalue if s is an lvalue, and is const if s is.
p->m A pointer to a structure or union. The member m of the pointed-to object. Always an lvalue.

Errors / undefined behavior: p->m with a null, dangling or uninitialized p is undefined behavior. Using . on a pointer, or -> on a non-pointer, is a compile error. Naming a member the type does not have is a compile error. In a union, reading a member other than the one last written reinterprets the bytes (allowed for type punning in C, but may produce a trap representation or an unspecified value).

Usage notes

  • a->b is exactly (*a).b. The parentheses are needed in the long form because . binds tighter than *.
  • Chains read left to right: list->head->next->value follows three pointers. Every pointer on the way must be valid; check for NULL if any link may be missing.
  • Nested structs use repeated dots (p.born.year); the arrow is needed only once for each pointer you cross.
  • A function can return a struct, and . works on the result (make().x), but the result is not an lvalue, so you cannot assign to it or take its address.
  • Member names have their own namespace per struct or union: two structs can both have a member called x.
  • No access control. C has no private members; all members are visible wherever the type is complete. A pointer to an incomplete (forward-declared) struct cannot use -> until the definition is visible.
  • Arrays inside structs are copied when the whole struct is assigned or passed by value, but a pointer member copies only the pointer.
  • Bit-fields can be accessed with . and -> like other members but you cannot take their address.
  • Anonymous structs and unions (standard since C11) let you write v.x directly for a member of an unnamed inner struct or union.