- 2072 Kartik · 3 marks
Dot and arrow operators
Short answer
Dot operator (.) accesses members of a structure variable, e.g., var.member. Arrow operator (->) accesses members through a structure pointer: ptr->member, equivalent to (*ptr).member. Use dot for structure itself, arrow for a pointer.
Explain dot and arrow operators for accessing the members of a structure.
Dot operator (.)
It is the member access operator used with a structure variable: variable.member.
Arrow operator (->)
It is used with a pointer to a structure: pointer->member. It is the same as dereferencing the pointer first, (*pointer).member. The brackets are required because . has higher precedence than *.
#include <stdio.h>
struct point {
int x, y;
};
int main(void)
{
struct point p = {3, 4};
struct point *ptr = &p;
printf("Using dot : %d %d\n", p.x, p.y);
printf("Using arrow : %d %d\n", ptr->x, ptr->y);
printf("Same as : %d %d\n", (*ptr).x, (*ptr).y);
return 0;
}
Output:
Using dot : 3 4
Using arrow : 3 4
Same as : 3 4
Use the dot when you have the structure itself, and the arrow when you have its address (for example in a function that receives a structure pointer, or with dynamic memory).
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Structure vs array; accessing members · Computer Programming, chapter 8
- Sending structure members to a function · Computer Programming, chapter 8
- Distance class relational operators · Object Oriented Programming, chapter 5