1: Defining and Declaring Structures
In C, a structure (struct) is a user-defined data type that allows you to combine different data types under a single name. It is useful for representing entities with multiple properties.
-
Structure Definition Syntax:
struct Student {
int id;
char name[50];
float gpa;
};
-
Structure Declaration and Initialization:
struct Student student1; // Declaration of a structure variable
student1.id = 101;
strcpy(student1.name, "John Doe");
student1.gpa = 3.8;
2: Nested Structures
You can define structures within other structures, allowing for hierarchical organization of data.
-
Example of Nested Structure:
struct Date {
int day;
int month;
int year;
};struct Employee {
int empId;
char empName[50];
struct Date joiningDate;
float salary;
};
-
Accessing Members of Nested Structures:
struct Employee emp;
emp.joiningDate.day = 15;
emp.joiningDate.month = 3;
emp.joiningDate.year = 2020;
3: Array of Structures
An array of structures allows you to store multiple instances of a structure in a contiguous memory block.
- Example of Array of Structures:
struct Book {
int bookId;
char title[100];
float price;
};struct Book library[5]; // Array of 5 Book structures
// Initializing array of structures
library[0].bookId = 1;
strcpy(library[0].title, "Introduction to C Programming");
library[0].price = 29.99;// Accessing array elements
printf("Book title: %sn", library[0].title);
4: Practice and Examples
- Practice Exercise:
- Write a program that defines a structure representing a point in 2D space (x, y coordinates) and calculates the distance between two points.
- Implement a function that takes an array of structures (representing students) as input, calculates the average GPA, and prints the student with the highest GPA.
- Develop a program that uses a nested structure to represent a university department (with professors and courses) and displays relevant information.
#include <stdio.h>
#include <math.h>
// Structure definition for a point in 2D space
struct Point {
float x;
float y;
};
// Function to calculate distance between two points
float distance(struct Point p1, struct Point p2) {
return sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2));
}
// Structure definition for a student
struct Student {
int id;
char name[50];
float gpa;
};
// Function to calculate average GPA and find student with the highest GPA
void analyzeStudents(struct Student students[], int numStudents) {
float totalGPA = 0.0;
float maxGPA = 0.0;
int maxIndex = -1;
for (int i = 0; i < numStudents; i++) {
totalGPA += students[i].gpa;
if (students[i].gpa > maxGPA) {
maxGPA = students[i].gpa;
maxIndex = i;
}
}
printf("Average GPA: %.2fn", totalGPA / numStudents);
if (maxIndex != -1) {
printf("Student with the highest GPA:n");
printf("ID: %dnName: %snGPA: %.2fn", students[maxIndex].id, students[maxIndex].name, students[maxIndex].gpa);
}
}
int main() {
// Example usage of Point structure
struct Point p1 = {0.0, 0.0};
struct Point p2 = {3.0, 4.0};
printf("Distance between points: %.2fn", distance(p1, p2));
// Example usage of Student structure
struct Student students[3] = {
{101, "Alice", 3.9},
{102, "Bob", 3.5},
{103, "Charlie", 4.0}
};
analyzeStudents(students, 3);
return 0;
}
Structures in C provide a powerful mechanism for organizing and manipulating complex data. Practice using structures to represent real-world entities and perform operations on structured data effectively. Arrays of structures allow you to work with collections of related data efficiently. Understanding nested structures enables you to model hierarchical relationships between entities in your programs. Experiment with different applications of structures to enhance your understanding of data modeling in C programming.