The dot (.) operator is used to access members of user-defined data types such as structures and unions. It allows direct access to a member using the structure or union variable.
- Used to access structure and union members through their variable names.
- Provides a simple and efficient way to read or modify member values.
#include <stdio.h>
struct Student {
int age;
};
int main() {
struct Student s;
s.age = 20;
printf("%d", s.age);
return 0;
}
Output
20
Syntax
name . member;
where,
- name: An instance of a structure or a union.
- member: member associated with the created structure or union.
Dot(.) operator with Nested Types
The dot operator can also be used to access the members of nested structures and unions. It can be done in the same way as done for the normal structure.
name . member1 . member2;
Precedence of dot (.) Operator
The dot (.) operator has the highest operator precedence in C Language and its associativity is from left to right.
Note: dot (.) operator can only be used with structure or unions in C language.
Working
- The dot (
.) operator accesses a member of a structure or union by combining the variable name with the member name. - Access the Structure/Union Variable: Create or use an existing structure or union variable.
- Use the Dot Operator: Write the variable name followed by the dot (
.) operator and the required member name. - Access or Modify the Member: The specified member can then be read or updated directly through the variable.
Examples
The below examples demonstrate how to use the dot (.) operator in different cases:
Accessing Members of Union
#include <stdio.h>
union A {
int x;
char c;
};
int main() {
union A a;
// Accessing and updating x member of union
a.x = 10;
printf("%d\n", a.x);
// Accessing and updating c member of union
a.c = 'Z';
printf("%c", a.c);
return 0;
}
Output
10 Z
Access Nested Member Structure
#include <stdio.h>
struct Base {
struct Child {
int i;
} child;
};
int main() {
struct Base base = { 12 };
// accessing nested structure member using dot operator
printf("%d", base.child.i);
return 0;
}
Output
12
Advantages
The dot (.) operator provides a simple and direct way to access members of structures and unions.
- Easy Member Access: Allows direct access to structure and union members using the structure or union variable.
- Improves Readability: Makes the code easier to understand by clearly identifying the accessed member.
- Efficient Access: Enables quick reading and modification of member values without using pointers.
Limitations
Although the dot operator is simple to use, it has certain limitations.
- Cannot Be Used with Pointers: The dot operator works only with structure or union variables. For pointers, the arrow (
->) operator must be used. - Limited to User-Defined Types: It can only access members of structures and unions, not primitive data types such as
int,char, orfloat. - Cannot Access Invalid Members: The specified member must exist in the structure or union; otherwise, a compilation error occurs.