- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Dot Operator
![]() Share with a Friend |
C Programming - C Dot Operator
C Dot Operator ( . )
The dot operator (.) in C is used to access members of a structure. It allows you to access and manipulate individual fields of a structure variable. This operator is fundamental when working with structures, which group related data into a single unit.
Syntax
C
structureVariable.memberName
- structureVariable: The name of the structure variable.
- memberName: The specific field within the structure.
Example: Using the Dot Operator
Defining and Accessing Structure Members
C
#include <stdio.h>
// Define a structure
struct Point {
int x;
int y;
};
int main() {
// Declare a structure variable
struct Point p1;
// Assign values to structure members using the dot operator
p1.x = 10;
p1.y = 20;
// Access and print structure members
printf("Point coordinates: x = %d, y = %d\n", p1.x, p1.y);
return 0;
}
Output:
Point coordinates: x = 10, y = 20
Key Characteristics
- Direct Access:
- The dot operator directly accesses the members of a structure variable.
- Example: p1.x retrieves the x member of the p1 structure.
- Read and Write Operations:
- You can use the dot operator to both read and assign values to structure members.
- Compile-Time Checking:
- The compiler ensures that the member exists in the structure during compilation.
Nested Structures
You can also use the dot operator to access members of nested structures.
Example: Nested Structures
C
#include <stdio.h>
// Define a nested structure
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
struct Date dob; // Nested structure for date of birth
};
int main() {
// Declare and initialize structure
struct Student student1 = {"John Doe", {15, 8, 2000}};
// Access nested structure members using the dot operator
printf("Name: %s\n", student1.name);
printf("Date of Birth: %02d/%02d/%d\n", student1.dob.day, student1.dob.month, student1.dob.year);
return 0;
}
Output:
Name: John Doe
Date of Birth: 15/08/2000
Comparison with Arrow Operator ( -> )
- The dot operator (.) is used for accessing members of a structure through a direct structure variable.
- The arrow operator (->) is used for accessing members through a pointer to a structure.
Common Errors
- Accessing Undefined Members:
- Trying to use the dot operator on non-existent members results in a compilation error.
- Example:
C
struct Point { int x, y; };
struct Point p;
p.z = 5; // Error: 'z' is not a member of 'Point'
- Using Dot Operator with a Pointer:
- If you use the dot operator with a pointer to a structure, it results in an error. Use the arrow operator instead.
C
struct Point *p;
p->x = 10; // Correct for pointers
Conclusion
The dot operator is a simple yet powerful tool for working with structures in C. It provides direct access to the members of a structure variable, enabling you to manipulate structured data effectively.
