In C programming, a structure is a user-defined data type that allows you to combine different data types into a single unit. It’s essentially a collection of variables (possibly of different types) grouped together under one name.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
data_type member3;
// more members
};
Why Use Structures?
Structures allow you to group related data together, even if the data types differ. For example, when dealing with a employee, you may need their name (a string), age (an integer), etc. . Instead of creating separate variables for each piece of information, you can bundle them into one logical unit called a structure.
Example: Define a Structure
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
};
This defines a structure Employee, which holds information about a employee, like its name, age, designation, and salary.
Declaring Structure Variables
Once you define the structure, you can create variables of that type.
struct Employee emp1, emp2; // Declare two variables of type 'Employee'
Alternatively, you can combine the structure definition and variable declaration in one line:
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
} emp1, emp2;
Accessing and assigning Structure Members
To access members (fields) of a structure, use the dot (.) operator.
emp1.name= "John";
emp1.age= 35;
emp1.designation = "Manager";
emp1.salary= 10000;
Structure Initialization
You can initialize a structure in various ways. Here are the most common methods:
a. Direct Initialization:
You can initialize structure members at the time of declaration:
struct Employee emp1= {"John", 35, "Manager", 15200.0};
b. Partial Initialization:
You don’t need to initialize all members. If you skip some members, they will be set to default values (0 for numeric types, empty string for char[]).
struct Employee emp1= {"Tom", 30};
c. Using Designated Initializers (C99 and later):
You can use designated initializers for better clarity:
struct Employee emp1 = {.name = "John", .age = 35, .designation = "Manager", .salary=15200.0};
Nested Structures
Structures can also contain other structures as members.
struct Address {
char street[100];
char city[50];
char state[50];
int zipCode;
};
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
struct Address address; // Nested structure
};
You can access the members of a nested structure using the dot operator:
struct Employee emp1;
strcpy(emp1.name, "John");
emp1.age= 35;
strcpy(emp1.designation, "Manager");
emp1.salary= 15200.0;
strcpy(emp1.address.street, "27 Main Street");
strcpy(emp1.address.city, "New Delhi");
strcpy(emp1.address.state, "Delhi");
emp1.address.zipCode = 10001;
Structures in Functions
Structures can be passed to functions in two ways:
Pass By Value: A copy of the structure is passed to the function.
Pass By Reference: A pointer to the structure is passed (more efficient, as no copy is made).
Example: Pass By Value
#include <stdio.h>
#include <string.h>
// Define the Employee structure
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
};
// Function to print the Employee details
void printEmployee(struct Employee emp) {
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Designation: %s\n", emp.designation);
printf("Salary: %.2f\n", emp.salary);
}
int main() {
// Initialize an Employee object
struct Employee emp1 = {"John", 35, "Manager", 10500.0};
// Passing structure by value to the function
printEmployee(emp1);
return 0;
}
Output:
Age: 35
Designation: Manager
Salary: 10500.00
Example: Pass By reference
#include <stdio.h>
#include <string.h>
// Define the Employee structure
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
};
// Function to print the Employee details (pass by reference using a pointer)
void printEmployee(struct Employee *emp) {
printf("Name: %s\n", emp->name);
printf("Age: %d\n", emp->age);
printf("Designation: %s\n", emp->designation);
printf("Salary: %.2f\n", emp->salary);
}
int main() {
// Initialize an Employee object
struct Employee emp1 = {"John", 35, "Manager", 10500.0};
// Passing the address (pointer) of emp1 to the function
printEmployee(&emp1);
return 0;
}
Output:
Age: 35
Designation: Manager
Salary: 10500.00