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.
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
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.
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
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->bis exactly(*a).b. The parentheses are needed in the long form because.binds tighter than*.- Chains read left to right:
list->head->next->valuefollows three pointers. Every pointer on the way must be valid; check forNULLif 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.xdirectly for a member of an unnamed inner struct or union.