”;
Dot (.) Operator in C
The dot (.) operator in C language is also known as “direction selection member“. It is used to select members of structure and union. The dot (.) operator is a binary operator that requires two operands (structure or union name and member name) and it has the highest operator precedence.
The dot (.) operator is useful when you want to access and manipulate the members (variables) of structure and union.
Using Dot (.) Operator
The dot (.) operator selects the members of the structure and union using the structure and union variable name. Here is the syntax of using the dot (.) operator to access members of a structure or union −
var.member;
Here, var is a variable of a certain struct or a union type, and member is one of the elements defined while creating the structure or union.
Example
A new derived data type is defined with struct keyword using the following syntax −
struct newtype { type elem1; type elem2; type elem3; ... ... };
You can then declare a variable of this derived data type as −
struct newtype var;
To access a certain member,
var.elem1;
Dot Operator with Structure (struct)
As discussed above, the dot (.) operator is used to access and manipulate the members of a structure.
Example
Let us declare a struct type named book and a struct variable. The following example shows how you can use the dot operator (.) to access the members in the book structure.
Take a look at the example −
#include <stdio.h> struct book { char title[10]; double price; int pages; }; int main (){ struct book b1 = {"Learn C", 675.50, 325}; printf("Title: %sn", b1.title); printf("Price: %lfn", b1.price); printf("No of Pages: %dn", b1.pages); printf("size of book struct: %d", sizeof(struct book)); return 0; }
Output
When you run this code, it will produce the following output −
Title: Learn C Price: 675.500000 No of Pages: 325 size of book struct: 32
Dot Operator with Union
The union keyword in C also lets you define a derived data type, very much similar to the struct keyword. However, unlike a struct variable, a variable of union type, only one of its members can contain a value at any given time.
The dot (.) operator is also used to access and manipulate the members of a union.
Example
You can also use the dot operator to access union member elements, as shown in this example −
#include <stdio.h> union Data { int i; float f; char str[20]; }; int main(){ union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming"); printf( "data.i : %dn", data.i); printf( "data.f : %fn", data.f); printf( "data.str : %sn", data.str); return 0; }
Output
When the above code is compiled and executed, it produces the following result −
data.i : 1917853763 data.f : 4122360580327794860452759994368.000000 data.str : C Programming
Dot Operator with Nested Structure
Nested structures are defined when one of the elements of a struct type is itself a composite representation of one or more types.
The dot operator can also be used to access the members of nested structures (and union types also). It can be done in the same way as done for the normal structure.
Suppose we have a nested structure as follows −
struct struct1 { var1; var2; struct struct2 { var3; var4; } s2; } s1;
In this case, the members of s1 are accessed as previously (as s1.var1 and s1.var2), and the members of inner struct are accessed as −
s1.s2.var3;
Example
In this example, we have an employee data type with one of its elements being the date of birth (dob). We shall declare the dob struct with three int types “d”, “m” and “y” inside the employee structure and its variable d1 is one of the elements of the outer type.
#include <stdio.h> struct employee { char name[10]; float salary; struct dob { int d, m, y; } d1; }; int main(){ struct employee e1 = {"Kiran", 25000, {12, 5, 1990}}; printf("Name: %sn", e1.name); printf("Salary: %fn", e1.salary); printf("Date of Birth: %d-%d-%dn", e1.d1.d, e1.d1.m, e1.d1.y); return 0; }
Output
Run the code and check its output −
Name: Kiran Salary: 25000.000000 Date of Birth: 12-5-1990
Accessing the Members Using the Arrow Operator
C also has another method to access the members of a struct variable. It can be done with the arrow operator (->) with the help of a pointer to the struct variable.
A new derived data type is defined with struct keyword as following syntax −
struct newtype { type elem1; type elem2; type elem3; ... ... };
You can then declare a variable of this derived data type, and its pointer as −
struct newtype var; struct newtype *ptr=&var;
To access a certain member through the pointer, use the syntax
ptr->elem1;
Example
Take a look at the following example −
#include <stdio.h> struct book { char title[10]; double price; int pages; }; int main (){ struct book b1 = {"Learn C", 675.50, 325}; struct book *strptr; strptr = &b1; printf("Title: %sn", strptr->title); printf("Price: %lfn", strptr->price); printf("No of Pages: %dn", strptr->pages); return 0; }
Output
When you run this code, it will produce the following output −
Title: Learn C Price: 675.500000 No of Pages: 325
Points to Note
It may be noted that −
- The dot operator (.) is used to access the struct elements via the struct variable.
- To access the elements via its pointer, we must use the indirection operator (->).
Accessing the Elements of a Nested Inner Structure
In case of a nested structure,
struct struct1 { var1; var2; struct struct2 { var3; var4; } s2; } s1; struct struct1 *ptr=&s1;
To access the elements of the inner structure of a nested structure, we use the following syntax −
ptr -> s2.var3;
Example
Take a look at the following example −
#include <stdio.h> struct employee { char name[10]; float salary; struct dob { int d, m, y; } d1; }; int main(){ struct employee e1 = {"Kiran", 25000, {12, 5, 1990}}; struct employee *ptr = &e1; printf("Name: %sn", ptr->name); printf("Salary: %fn", ptr->salary); printf("Date of Birth: %d-%d-%dn", ptr->d1.d, ptr->d1.m, ptr->d1.y); return 0; }
Output
Run the code and check its output −
Name: Kiran Salary: 25000.000000 Date of Birth: 12-5-1990
”;