• C Programming Video Tutorials

Dot (.) Operator in C



In C language, you can define a derived data type with struct and union keywords. A derived or user-defined data type that groups together member elements of different types.

The dot operator is a "member selection operator", when used with the struct or union variable. The dot (.) operator has the highest operator precedence in C Language and its associativity is from left to right.

Syntax

Here is the syntax of using the dot operator in a C program −

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.

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 Struct

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.

Example

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: %s\n", b1.title);
   printf("Price: %lf\n", b1.price);
   printf("No of Pages: %d\n", 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 mush 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.

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 : %d\n", data.i);
   printf( "data.f : %f\n", data.f);
   printf( "data.str : %s\n", 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: %s\n", e1.name);
   printf("Salary: %f\n", e1.salary);
   printf("Date of Birth: %d-%d-%d\n", 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: %s\n", strptr->title);
   printf("Price: %lf\n", strptr->price);
   printf("No of Pages: %d\n", 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: %s\n", ptr->name);
   printf("Salary: %f\n", ptr->salary);
   printf("Date of Birth: %d-%d-%d\n", 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
Advertisements