- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Dot (.) Operator in C
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: %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 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 : %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