In C programming, a structure (or struct) is a user-defined data type that allows grouping of different data types together.
Structures help organize complex data by grouping variables (called members) under a single name, allowing for more readable and maintainable code, especially when dealing with related information.
1. Defining a Structure
To define a structure, use the struct keyword followed by the structure name and a set of braces containing the members of the structure.
struct StructureName { data_type member1; data_type member2; // Add more members as needed };
- StructureName: The name of the structure.
- data_type: Data type of each member within the structure.
- member: The individual variable within the structure.
2. Basic Example of Structure Definition
Here’s a simple structure called Person, which groups information about a person’s name, age, and height.
#include <stdio.h> struct Person { char name[50]; int age; float height; }; int main() { struct Person person1; // Assign values to person1 members person1.age = 25; person1.height = 5.9; snprintf(person1.name, sizeof(person1.name), "John Doe"); // Print person1 data printf("Name: %s\n", person1.name); printf("Age: %d\n", person1.age); printf("Height: %.1f\n", person1.height); return 0; }
Explanation:
- struct Person defines a structure with three members: name, age, and height.
- person1 is a variable of type struct Person.
- Members are accessed using the dot (.) operator.
Output:
Name: John Doe Age: 25 Height: 5.9
3. Initializing Structure Variables
Structures can be initialized when they are declared.
#include <stdio.h> struct Book { char title[100]; char author[50]; int year; }; int main() { struct Book book1 = {"The Great Gatsby", "F. Scott Fitzgerald", 1925}; printf("Title: %s\n", book1.title); printf("Author: %s\n", book1.author); printf("Year: %d\n", book1.year); return 0; }
Explanation:
- struct Book book1 = {…} initializes the structure members in the order they are defined.
- book1 is initialized directly at declaration.
Output:
Title: The Great Gatsby Author: F. Scott Fitzgerald Year: 1925
4. Accessing Structure Members
Structure members can be accessed using the dot (.) operator for individual structure variables.
#include <stdio.h> struct Rectangle { int length; int width; }; int main() { struct Rectangle rect = {10, 5}; printf("Length: %d\n", rect.length); printf("Width: %d\n", rect.width); printf("Area: %d\n", rect.length * rect.width); return 0; }
Explanation:
- rect.length and rect.width access the length and width members of rect.
- The area is calculated by multiplying the two members.
Output:
Length: 10 Width: 5 Area: 50
5. Array of Structures
You can create an array of structures to store multiple instances of the same structure type.
#include <stdio.h> struct Student { char name[50]; int age; float grade; }; int main() { struct Student students[3] = { {"Alice", 20, 3.9}, {"Bob", 21, 3.6}, {"Charlie", 22, 3.8} }; for (int i = 0; i < 3; i++) { printf("Name: %s, Age: %d, Grade: %.1f\n", students[i].name, students[i].age, students[i].grade); } return 0; }
Explanation:
- struct Student students[3] defines an array of Student structures.
- The loop iterates over the array, printing each student’s details.
Output:
Name: Alice, Age: 20, Grade: 3.9 Name: Bob, Age: 21, Grade: 3.6 Name: Charlie, Age: 22, Grade: 3.8
6. Structure Pointers
You can use pointers to structures to dynamically allocate memory or pass structures to functions by reference.
#include <stdio.h> struct Point { int x; int y; }; int main() { struct Point p = {10, 20}; struct Point *ptr = &p; printf("x = %d, y = %d\n", ptr->x, ptr->y); return 0; }
Explanation:
- ptr is a pointer to p.
- ptr->x and ptr->y use the arrow operator (->) to access members of the structure through the pointer.
Output:
x = 10, y = 20
7. Passing Structures to Functions
Structures can be passed to functions by value or by reference.
Passing by Value
#include <stdio.h> struct Circle { float radius; }; void printArea(struct Circle c) { float area = 3.14159 * c.radius * c.radius; printf("Area: %.2f\n", area); } int main() { struct Circle c = {5.0}; printArea(c); return 0; }
Explanation:
- printArea receives a Circle structure by value, meaning it operates on a copy of the structure.
Output:
Area: 78.54
Passing by Reference
Passing by reference (using pointers) allows the function to modify the original structure.
#include <stdio.h> struct Circle { float radius; }; void doubleRadius(struct Circle *c) { c->radius *= 2; // Modifies the original radius } int main() { struct Circle c = {5.0}; doubleRadius(&c); printf("Doubled Radius: %.2f\n", c.radius); return 0; }
Explanation:
- doubleRadius receives a pointer to a Circle structure, allowing it to modify the original radius.
Output:
Doubled Radius: 10.00
8. Structures Within Structures (Nested Structures)
Structures can contain other structures as members.
#include <stdio.h> struct Date { int day; int month; int year; }; struct Employee { char name[50]; struct Date birthDate; }; int main() { struct Employee emp = {"Alice", {15, 5, 1990}}; printf("Employee Name: %s\n", emp.name); printf("Birth Date: %02d/%02d/%d\n", emp.birthDate.day, emp.birthDate.month, emp.birthDate.year); return 0; }
Explanation:
- Employee has a Date structure as a member.
- emp.birthDate.day, emp.birthDate.month, and emp.birthDate.year access the nested structure members.
Output:
Employee Name: Alice Birth Date: 15/05/1990
9. Typedef with Structures
Using typedef with structures simplifies code by allowing the structure to be referred to by a custom type name.
#include <stdio.h> typedef struct { char title[50]; char author[50]; int year; } Book; int main() { Book b = {"1984", "George Orwell", 1949}; printf("Title: %s\n", b.title); printf("Author: %s\n", b.author); printf("Year: %d\n", b.year); return 0; }
Explanation:
- typedef struct { … } Book; defines a custom type Book, so struct keyword isn’t needed when declaring variables.
Output:
Title: 1984 Author: George Orwell Year: 1949
10. Summary Table of Structure Examples
Example | Description | Code Snippet |
---|---|---|
Basic structure definition | Defines a structure with multiple members | struct Person { char name[50]; int age; float height; }; |
Initializing a structure | Assigns values to a structure during declaration | struct Book book1 = {“Title”, “Author”, 2021}; |
Array of structures | Stores multiple instances in an array | struct Student students[3] = { … }; |
Structure pointers | Accesses members through pointers | struct Point *ptr = &p; ptr->x; |
Passing structure to function | Passes a structure by value or reference | void print(struct Circle c); / void doubleRadius(struct Circle *c); |
Nested structures | Structure containing another structure | struct Employee { struct Date birthDate; }; |
typedef with structures | Simplifies structure usage with custom type | typedef struct { … } Book; |
Complete Example
This example demonstrates using structures to manage a simple student database.
#include <stdio.h> typedef struct { char name[50]; int age; float gpa; } Student; void printStudent(Student s) { printf("Name: %s\n", s.name); printf("Age: %d\n", s.age); printf("GPA: %.2f\n", s.gpa); } int main() { Student students[3] = { {"Alice", 20, 3.9}, {"Bob", 22, 3.7}, {"Charlie", 19, 3.8} }; for (int i = 0; i < 3; i++) { printf("Student %d:\n", i + 1); printStudent(students[i]); printf("\n"); } return 0; }
Explanation:
- Student is a structure with name, age, and gpa.
- students is an array of Student structures.
- printStudent takes a Student and prints their information.
Output:
Student 1: Name: Alice Age: 20 GPA: 3.9 Student 2: Name: Bob Age: 22 GPA: 3.7 Student 3: Name: Charlie Age: 19 GPA: 3.8
Structures in C provide an organized way to group related data, making it easier to manage complex information.