C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we'll explore the concept of structure arrays in the C programming language. A structure array is an array of structure variables, which allows you to store and manipulate a collection of structured data.
Structure Array Syntax
To define an array of structures, you can use the following syntax:
struct structure_name array_name[array_size];
Examples
Here are some examples demonstrating the use of structure arrays in C:
struct Student { int id; char name[50]; float marks; };
#include <stdio.h> struct Student { int id; char name[50]; float marks; }; int main() { // Declare and initialize a structure array struct Student students[3] = { {1, "John Doe", 95.0}, {2, "Jane Doe", 90.0}, {3, "Jim Smith", 89.0} }; for (int i = 0; i < 3; i++) { printf("Student %d: %s (ID: %d) scored %.2f\n", i + 1, students[i].name, students[i].id, students[i].marks); } return 0; }
In this example, we declare and initialize a structure array students
with values for the id
, name
, and marks
members. We then use a loop to print the information of each student in the array.
#include <stdio.h> struct Student { int id; char name[50]; float marks; }; // Function declaration float calculate_average(struct Student students[], int n); int main() { struct Student students[3] = { {1, "John Doe", 95.0}, {2, "Jane Doe", 90.0}, {3, "Jim Smith", 89.0} }; float average_marks = calculate_average(students, 3); printf("The average marks of the students is %.2f\n", average_marks); return 0; } // Function definition float calculate_average(struct Student students[], int n) { float sum = 0; for (int i = 0; i < n; i++) { sum += students[i].marks; } return sum / n; }
In this example, we define a function calculate_average()
that takes a structure array and its size as arguments, then calculates the average marks of the students. We call this function in the main()
function to compute and print the average marks.
Conclusion
In this tutorial, we explored the concept of structure arrays in the C programming language. Structure arrays are useful when you need to store and manipulate a collection of structured data. They provide a convenient way to organize and manage related data, which is crucial for writing clean, efficient, and maintainable code.
Declaring and initializing arrays of structures in C:
#include <stdio.h> // Declaring and initializing arrays of structures in C struct Point { int x; int y; }; int main() { // Array of structures struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}}; // Accessing elements in the array of structures printf("First point: (%d, %d)\n", points[0].x, points[0].y); return 0; }
Accessing and modifying elements in a structure array:
#include <stdio.h> // Accessing and modifying elements in a structure array struct Student { char name[50]; int age; }; int main() { // Array of structures struct Student students[3] = {{"Alice", 20}, {"Bob", 22}, {"Charlie", 21}}; // Accessing and modifying elements printf("Before modification: %s is %d years old.\n", students[1].name, students[1].age); // Modifying an element students[1].age = 23; printf("After modification: %s is now %d years old.\n", students[1].name, students[1].age); return 0; }
Using loops for iteration with structure arrays in C:
for
or while
, can be used for efficient iteration over a structure array.#include <stdio.h> // Using loops for iteration with structure arrays in C struct Employee { char name[50]; int id; }; int main() { // Array of structures struct Employee employees[3] = {{"Alice", 101}, {"Bob", 102}, {"Charlie", 103}}; // Iterating over the array using a loop for (int i = 0; i < 3; ++i) { printf("Employee %d: %s (ID: %d)\n", i + 1, employees[i].name, employees[i].id); } return 0; }
Sorting and searching techniques for structure arrays in C:
Description: Sorting and searching algorithms can be applied to structure arrays based on specific criteria.
Example (Sorting):
#include <stdio.h> #include <stdlib.h> #include <string.h> // Sorting structure array based on name int compareNames(const void *a, const void *b) { return strcmp(((struct Student *)a)->name, ((struct Student *)b)->name); } int main() { // Array of structures struct Student students[3] = {{"Charlie", 21}, {"Alice", 20}, {"Bob", 22}}; // Sorting the array based on name qsort(students, 3, sizeof(struct Student), compareNames); // Displaying sorted array for (int i = 0; i < 3; ++i) { printf("Student %d: %s (Age: %d)\n", i + 1, students[i].name, students[i].age); } return 0; }
Example (Searching):
#include <stdio.h> #include <string.h> // Searching for a student by name struct Student *searchStudentByName(const char *targetName, struct Student *array, int size) { for (int i = 0; i < size; ++i) { if (strcmp(array[i].name, targetName) == 0) { return &array[i]; } } return NULL; // Not found } int main() { // Array of structures struct Student students[3] = {{"Alice", 20}, {"Bob", 22}, {"Charlie", 21}}; // Searching for a student by name const char *targetName = "Bob"; struct Student *foundStudent = searchStudentByName(targetName, students, 3); if (foundStudent != NULL) { printf("Student found: %s (Age: %d)\n", foundStudent->name, foundStudent->age); } else { printf("Student not found.\n"); } return 0; }
Passing structure arrays to functions in C language:
#include <stdio.h> // Passing structure arrays to functions in C struct Point { int x; int y; }; // Function to calculate the sum of x and y coordinates in an array of points int sumCoordinates(struct Point array[], int size) { int sum = 0; for (int i = 0; i < size; ++i) { sum += array[i].x + array[i].y; } return sum; } int main() { // Array of structures struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}}; // Passing the array to a function int totalSum = sumCoordinates(points, 3); printf("Total sum of coordinates: %d\n", totalSum); return 0; }
Dynamic memory allocation for structure arrays in C:
malloc
allows flexibility in managing structure arrays.#include <stdio.h> #include <stdlib.h> // Dynamic memory allocation for structure arrays in C struct Student { char name[50]; int age; }; int main() { int numStudents; printf("Enter the number of students: "); scanf("%d", &numStudents); // Dynamic memory allocation for an array of structures struct Student *students = (struct Student *)malloc(numStudents * sizeof(struct Student)); if (students != NULL) { // Input data for each student for (int i = 0; i < numStudents; ++i) { printf("Enter name and age for student %d: ", i + 1); scanf("%s %d", students[i].name, &students[i].age); } // Displaying entered data for (int i = 0; i < numStudents; ++i) { printf("Student %d: %s (Age: %d)\n", i + 1, students[i].name, students[i].age); } free(students); // Freeing dynamically allocated memory } else { printf("Memory allocation failed.\n"); } return 0; }
C code examples demonstrating structure array usage:
#include <stdio.h> // C code examples demonstrating structure array usage struct Car { char make[20]; char model[20]; int year; }; int main() { // Array of structures struct Car fleet[3] = { {"Toyota", "Camry", 2020}, {"Honda", "Civic", 2019}, {"Ford", "Fusion", 2021} }; // Displaying information about each car for (int i = 0; i < 3; ++i) { printf("Car %d: %d %s %s\n", i + 1, fleet[i].year, fleet[i].make, fleet[i].model); } return 0; }
Multidimensional structure arrays and nested structures in C:
#include <stdio.h> // Multidimensional structure arrays and nested structures in C struct Date { int day; int month; int year; }; struct Person { char name[50]; int age; struct Date birthdate; }; int main() { // Multidimensional array of structures struct Person family[2][2] = { {{"Alice", 25, {10, 5, 1998}}, {"Bob", 30, {15, 7, 1992}}}, {{"Charlie", 22, {8, 3, 2000}}, {"Diana", 28, {20, 11, 1994}}} }; // Accessing nested structures printf("Birthdate of %s: %d/%d/%d\n", family[0][0].name, family[0][0].birthdate.day, family[0][0].birthdate.month, family[0][0].birthdate.year); return 0; }