Structure Real-Time Examples in C

Structure Real-Time Examples in C Language

In this article, I will discuss Structure Real-time Examples in C Language with Examples. Please read our previous article discussing Structure using Pointer in C with Examples. At the end of this article, you will understand the following examples using Structure in C.

  1. Student Database System
  2. Bank Account Management
  3. Employee Management System
  4. University Course Management
  5. Contact Management System
  6. Vehicle Information System
  7. Dynamic Employee Management System
  8. Singly Linked List
  9. Dynamic Array of Structures
Structure Real-time Examples in C Language

Using structures in C can greatly enhance code organization and readability, particularly when grouping different data types is beneficial. Here are some real-time examples that demonstrate the use of structures without involving pointers:

Example: Student Database System

We’ll create a simple student database system using the structures in this example. This system will hold student information, such as ID, name, and grades.

Struct Definition:
struct Student {
    int id;
    char name[50];
    float grade;
};
Function to Display Student Information:
void displayStudent(struct Student stu) {
    printf("Student ID: %d\n", stu.id);
    printf("Student Name: %s\n", stu.name);
    printf("Student Grade: %.2f\n", stu.grade);
}
Main Function:
int main() {
    struct Student student1;

    student1.id = 1;
    strcpy(student1.name, "Alice Smith");
    student1.grade = 3.5;

    displayStudent(student1);

    return 0;
}

In this example, we define a Student structure, populate it with data, and display the information using a function. This approach can be extended to manage many student records, including features like adding, updating, and deleting student data.

Example: Bank Account Management

Here, we’ll visually represent a bank account management system. We’ll use structures to represent bank accounts and perform simple operations.

Struct Definition:
struct BankAccount {
    int accountNumber;
    char holderName[100];
    double balance;
};
Function to Deposit Money:
void deposit(struct BankAccount *account, double amount) {
    account->balance += amount;
}
Function to Withdraw Money:
void withdraw(struct BankAccount *account, double amount) {
    if (amount <= account->balance) {
        account->balance -= amount;
    } else {
        printf("Insufficient funds.\n");
    }
}
Main Function:
int main() {
    struct BankAccount account1 = {123456, "John Doe", 1000.0};

    printf("Initial Balance: $%.2f\n", account1.balance);

    deposit(&account1, 500.0);
    printf("Balance after deposit: $%.2f\n", account1.balance);

    withdraw(&account1, 200.0);
    printf("Balance after withdrawal: $%.2f\n", account1.balance);

    return 0;
}

In this example, we define a BankAccount structure and perform deposit and withdrawal operations. The functions deposit and withdraw modify the account’s balance. This kind of structure-based approach is foundational in creating more complex banking systems.

Example: Employee Management System

In an employee management system, structures can store and manage employee information, such as their ID, name, department, and salary.

Structure Definition:
struct Employee {
    int id;
    char name[50];
    char department[50];
    double salary;
};
Function to Display Employee Information:
void printEmployeeInfo(struct Employee emp) {
    printf("Employee ID: %d\n", emp.id);
    printf("Employee Name: %s\n", emp.name);
    printf("Department: %s\n", emp.department);
    printf("Salary: %.2f\n", emp.salary);
}
Main Function:
int main() {
    struct Employee emp1 = {1, "Alice Johnson", "IT", 75000.00};

    printEmployeeInfo(emp1);

    return 0;
}

This program defines an Employee structure, initializes an employee’s data, and displays it. In a real system, you would have more functionality, like adding, updating, or deleting employee records.

Example: University Course Management

In this example, we use structures to represent and manage university courses and student enrollment.

Structure Definitions:
struct Course {
    int id;
    char title[100];
    int credits;
};

struct Student {
    int id;
    char name[50];
    struct Course enrolledCourses[5];
    int enrolledCourseCount;
};
Function to Enroll in a Course:
void enrollCourse(struct Student *student, struct Course course) {
    if (student->enrolledCourseCount < 5) {
        student->enrolledCourses[student->enrolledCourseCount++] = course;
    } else {
        printf("Cannot enroll in more than 5 courses.\n");
    }
}
Main Function:
int main() {
    struct Student student1 = {1001, "John Doe", {}, 0};
    struct Course course1 = {101, "Introduction to Computer Science", 4};
    struct Course course2 = {102, "Data Structures", 3};

    enrollCourse(&student1, course1);
    enrollCourse(&student1, course2);

    // Display student's enrolled courses
    printf("Courses enrolled by %s:\n", student1.name);
    for (int i = 0; i < student1.enrolledCourseCount; i++) {
        printf("%s\n", student1.enrolledCourses[i].title);
    }

    return 0;
}

In this program, we define structures for Course and Student. A student can enroll in multiple courses. We demonstrate enrolling a student in courses and then display the enrolled courses.

Example: Contact Management System

In a contact management system, structures can be used to store and manage information about individuals, such as their name, phone number, and email address.

Structure Definition:
struct Contact {
    char name[100];
    char phoneNumber[15];
    char email[100];
};
Function to Display Contact Information:
void displayContact(struct Contact c) {
    printf("Name: %s\n", c.name);
    printf("Phone Number: %s\n", c.phoneNumber);
    printf("Email: %s\n", c.email);
}
Main Function:
int main() {
    struct Contact contact1;
    strcpy(contact1.name, "Alice Johnson");
    strcpy(contact1.phoneNumber, "123-456-7890");
    strcpy(contact1.email, "alice.johnson@example.com");

    displayContact(contact1);

    return 0;
}

In this example, we define a Contact structure, populate it with data, and display the information. This approach can be extended to manage a list of contacts, including features like adding, updating, and deleting contact data.

Example: Vehicle Information System

Here, we’ll create a representation of a vehicle information system. We’ll use structures to represent vehicles and store details like the make, model, and year.

Structure Definition:
struct Vehicle {
    char make[50];
    char model[50];
    int year;
};
Function to Display Vehicle Information:
void displayVehicle(struct Vehicle v) {
    printf("Make: %s\n", v.make);
    printf("Model: %s\n", v.model);
    printf("Year: %d\n", v.year);
}
Main Function:
int main() {
    struct Vehicle car = {"Toyota", "Corolla", 2020};

    displayVehicle(car);

    return 0;
}

In this program, we define a Vehicle structure and display its details. This structure-based approach is foundational in creating more complex systems for managing vehicle-related information, such as fleets in a transportation company.

Example: Dynamic Employee Management System

This example demonstrates a simple employee management system using structures and pointers, where each employee’s information is dynamically allocated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int id;
    char name[100];
    float salary;
} Employee;

void printEmployee(Employee *e) {
    printf("ID: %d, Name: %s, Salary: %.2f\n", e->id, e->name, e->salary);
}

int main() {
    Employee *emp = malloc(sizeof(Employee));
    if (emp == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    emp->id = 123;
    strcpy(emp->name, "John Doe");
    emp->salary = 50000.0;

    printEmployee(emp);

    free(emp);
    return 0;
}
Example: Singly Linked List

A singly linked list is a classic example of using self-referential structures with pointers. It’s a linear collection of data elements, where each element points to the next.

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node* createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (!newNode) return NULL;
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void printList(Node *head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

int main() {
    Node *head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);

    printList(head);

    // Freeing the list (not shown for brevity)
    return 0;
}
Example: Dynamic Array of Structures

This example shows how to create and manipulate a dynamic array of structures.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x;
    int y;
} Point;

int main() {
    int n = 3;
    Point *points = (Point *)malloc(n * sizeof(Point));
    if (!points) {
        perror("Memory allocation failed");
        return 1;
    }

    // Initializing points
    for (int i = 0; i < n; i++) {
        points[i].x = i;
        points[i].y = i * i;
    }

    // Printing points
    for (int i = 0; i < n; i++) {
        printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
    }

    free(points);
    return 0;
}

These examples demonstrate how structures in C can be used to model real-world entities and manage data efficiently. Structures provide a way to group related data together, making it easier to manipulate and maintain. In practice, such applications would be more complex and feature-rich, often involving dynamic memory allocation, file handling, and more sophisticated data management techniques.

In the next article, I will discuss Union in C Language with Examples. In this article, I explain Structure Real-Time Examples in C Language. I hope you enjoy this Structure Real-Time Examples in C Language article. I would like to have your feedback. Please post feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *